FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


php classes - simple tutorial please





DoctorBeaver
Can anyone explain what they are & how to use them in terms that a slow-witted rodent can understand?

I've looked all over the net & all I can find are sites that give examples or offer free class scripts without explaining the whats, whys & wherefores of how to use them etc.

Any help much appreciated.
gstreams
understand classes as objects.

therefore classes can have properties and do actions:

here's a short example:

Code:

class classname{

   var $prop1;
   var $prop2;

   function action1($arg1, $arg2="default_value")
   {
       $this->prop1= $arg1;
       $this->prop2= $arg2;
   }
   
   function debug(){
         echo "prop1:".$this->prop1."<br>";
         echo "prop2:".$this->prop2."<br>";
   }

 
} // end of class Vegetable


now you are haveing a class with the following name : classname

it has 2 properties : prop1, prop2.
and 2 actions called : action1 that can accept 2 arguments.

this is how you should use it:

Code:


object=new classname;
object->action1("first propery","seccond property");
object->debug();



the first line creates an object;
the seccond line calls action1. this action assigns the values passed to the properties prop1 and prop2.
the last line displays the properties values; in this case :"first propery","seccond property"

read more here : http://ro2.php.net/manual/en/ref.classobj.php
avk
Hello

i didn't understand the class thing.
Can anyone explain me in more detail
DoctorBeaver
gstreams - but what does it do & how? How does it differ from merely using a function?

Also, what does -> mean? Is it the same as => ?

I've just come across this:-
Code:
<?php
class Cart {
   var $items;  // Items in our shopping cart

   // Add $num articles of $artnr to the cart

   function add_item($artnr, $num) {
       $this->items[$artnr] += $num;
   }

   // Take $num articles of $artnr out of the cart

   function remove_item($artnr, $num) {
       if ($this->items[$artnr] > $num) {
           $this->items[$artnr] -= $num;
           return true;
       } elseif ($this->items[$artnr] == $num) {
           unset($this->items[$artnr]);
           return true;
       } else {
           return false;
       }
   }
}
?>


What is the advantage of this over simply using the 2 functions separately?
krazycapital
This is a simple script on how to use it:
Code:
<?php
class Cart {
   var $items;  // Items in our shopping cart

   // Add $num articles of $artnr to the cart

   function add_item($artnr, $num) {
      $this->items[$artnr] += $num;
   }

   // Take $num articles of $artnr out of the cart

   function remove_item($artnr, $num) {
      if ($this->items[$artnr] > $num) {
         $this->items[$artnr] -= $num;
         return true;
      } elseif ($this->items[$artnr] == $num) {
         unset($this->items[$artnr]);
         return true;
      } else {
         return false;
      }
   }

   //Output items
   function output_items() {
      print_r($this->items);
   }
}

//You have to make the object.
$cart = new Cart();
//This adds 3 items of the item "pizza"
$cart->add_item("pizza",3);
//This takes away 1 pizza
$cart->remove_item("pizza",1);
/*This would output an array of the items. The array would look like this for above.
Array
(
    [pizza] => 2
)*/
$cart->output_items();
?>


Once you learn how to use it, it's amazing how much easier OOP is. I can't think of a way to do that so easily with just three functions, without having to have a lot of extra variables.
DoctorBeaver
Thanks, Krazy. I think I get it. I'll mess around with it & see what happens.
Related topics
How To : Secure Your PHP Website
Complete Tutorial LINKS for every language
Mail with PHP (Simple Tutorial)
Tutorial: PHP Installed Modules Dynamic Reference Tool
Tutorial: Image Rollovers w/ Javascript
[Fireworks] Simple Av Tut
[PhP] News Posting Tutorial (code, actually ^^')
Looking to make an online database using php and mysql
PHP Mysql admin area? please help
help: What is wrong with my online form?
My First CSS Tutorial
Install a Simple Machines Forum (SMF)
Any free PHP or MYSQL Tutorial
Favorite PHP Classes and Scripts
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.