Help - Search - Members - Calendar
Full Version: Little Php Oop Help
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
ap_
I was wondering if someone could giveme a hand on finding a good php oop tutorial.

thnks by the way!
Alex
http://php.net/oop5 is a good summary of what OOP provides (in PHP5 of course, since PHP4 is now pretty much archaic).
MrTouz
ok so in simple what does oop do ?
ap_
QUOTE(MrTouz @ Dec 8 2007, 12:22 PM) *
ok so in simple what does oop do ?


i was kind of lost.

jeje thankyou!
Ed
Think of an object as you would do in everyday life, in programming, a class (which is used to outline an object) is an encapsulation of the methods and properties that define that particular object, let's take a beverage for example, a beverage could have two properties, how much remains and the type of beverage, the methods could be to drink the beverage or maybe refill the beverage, the drinking of the beverage would naturally decrease the remaining amount, both of these actions could be performed in a method. Here's an example of how that might look:

CODE
<?php
class beverage
{
   private $remaining;
   private $type;

   public function __construct($type)
   {
      $this->type = $type;
      $this->remaining = 100;
   }

   public function consume($amount)
   {
      $remaining = $this->remaining - $amount;

      if($remaining >= 0)
      {
         $this->remaining = $remaining;
         return true;
      }
      else
      {
         return false;
      }
   }

   public function exists()
   {
      return ($remaining > 0);
   }

   public function refill()
   {
      $this->remaining = 100;
   }

   public function getBeverage()
   {
      return $this->type;
   }

   public function getRemaining()
   {
      return $this->remaining;
   }

   public function __destruct()
   {
      $this->remaining = 0;
   }
}

$beverages = array();

$types = array
(
   'wine',
   'water',
   'beer'
);

foreach($types as $type)
{
   $beverages[$type] = new beverage($type);
}

$consume = array(15, 20, 40, 15, 40, 10);

foreach($beverages as $beverage)
{
   foreach($consume as $amount)
   {
      if($beverage->consume($amount))
      {
         printf("Consumed %d%% of %s, %d%% remaining.<br />\n", $amount, $beverage->getBeverage(), $beverage->getRemaining());
      }
      else
      {
         printf("I can't consume %d%% -- not enough %s exists: %d%% remaining.<br />\n", $amount, $beverage->getBeverage(), $beverage->getRemaining());
      }
  
      if($beverage->getRemaining() == 0)
      {
         printf("There is no more %s left.<br />\n", $beverage->getBeverage());
         break;
      }
   }
}

echo "Time for a refill!<br />\n";

foreach($beverages as $beverage)
{
   $beverage->refill();

   printf("Refilled. I now have %d%% %s remaining<br />\n", $beverage->getRemaining(), $beverage->getBeverage());
}
?>


Run it first to see what's happening.

Firstly, I better point out this is written in PHP5 so running this via a PHP4 parser will throw a whole bundle of errors. That out of the way, I'll go into a little detail on how classes are written.

Properties

There are two properties, '$remaining' and '$type', properties are native to the class, notice the access keyword 'private', it's a personal convention that I do not allow access to properties from an instantiated object, so these values are only accessible within the class. The properties in this class keep track of the beverage type and the remaining beverage left.

Methods

Methods are the 'functions' to which the class possesses, for example 'consume' is a method, so are 'getRemaining' and 'getBeverage'. If you're at all familiar with functions, then those should cause no problems. All my methods in this class use the access keyword 'public', basically this means any class / instantiated object can access those methods, if I were to define them like my properties as 'private', I could not call them outside of the class.

Constructors and destructors

The constructor '__construct' is called when you create a new instance of the object (with the 'new' keyword). In this example, the constructor is used to set the beverage type. The destructor in this example is pretty obsolete, but for conventions sake one has been included. Destructors are called when an object is unset, so for example in a database object, a destructor could be used to to close the database connection.

Moving on... inheritance

If you're still not clear to what object orientated programming is about, then I suggest you re-read the above, ask a question or stop here. This next section will go into something that requires you to understand the aforementioned.

Think of inheritance as a young child / mother scenario, the young child needs their mother, in proper terminology, the 'parent', 'child' relationship. So in our beverage example needs a container, so let's introduce a container object, we'll want a type and colour property:

CODE
<?php
class beverage extends container
{
   private $remaining;
   private $type;

   public function __construct($type, $container, $colour)
   {
      $this->type = $type;
      $this->remaining = 100;

      parent::__construct($container, $colour);
   }

   public function consume($amount)
   {
      $remaining = $this->remaining - $amount;

      if($remaining >= 0)
      {
         $this->remaining = $remaining;
         return true;
      }
      else
      {
         return false;
      }
   }

   public function exists()
   {
      return ($remaining > 0);
   }

   public function refill()
   {
      $this->remaining = 100;
   }

   public function getBeverage()
   {
      return $this->type;
   }

   public function getRemaining()
   {
      return $this->remaining;
   }

   public function __destruct()
   {
      $this->remaining = 0;
   }
}

class container
{
   private $type;
   private $colour;
  
   public function __construct($type, $colour)
   {
      $this->type = $type;
      $this->colour = $colour;
   }

   public function getColour()
   {
      return $this->colour;
   }

   public function getContainer()
   {
      return $this->type;
   }
}

$beverages = array();

$types = array
(
   array('wine', 'flute', 'transparent'),
   array('water', 'glass', 'orange'),
   array('beer', 'bottle', 'brown')
);

foreach($types as $type)
{
   list($beverageType, $container, $colour) = $type;
   $beverages[$beverageType] = new beverage($beverageType, $container, $colour);
}

$consume = array(15, 20, 40, 15, 40, 10);

foreach($beverages as $beverage)
{
   foreach($consume as $amount)
   {
      if($beverage->consume($amount))
      {
         printf("Consumed %d%% of %s, %d%% remaining in the %s %s.<br />\n", $amount, $beverage->getBeverage(), $beverage->getRemaining(), $beverage->getColour(), $beverage->getContainer());
      }
      else
      {
         printf("I can't consume %d%% -- not enough %s exists: %d%% remaining in the %s %s.<br />\n", $amount, $beverage->getBeverage(), $beverage->getRemaining(), $beverage->getColour(), $beverage->getContainer());
      }
  
      if($beverage->getRemaining() == 0)
      {
         printf("There is no more %s left in the %s %s.<br />\n", $beverage->getBeverage(), $beverage->getColour(), $beverage->getContainer());
         break;
      }
   }
}

echo "Time for a refill!<br />\n";

foreach($beverages as $beverage)
{
   $beverage->refill();

   printf("Refilled. I now have %d%% %s remaining<br />\n", $beverage->getRemaining(), $beverage->getBeverage());
}
?>


Now you'll notice firstly the addition of the container class, very basic object, the constructor sets the type and colour properties, the destruct is completely obsolete and remains purely for conventional reasons. You might question why we do not just rename the variables (so they don't conflict) and have it within the beverage class, well you could do, but thinking from an object perspective a container is not the same as a beverage, admittedly having a beverage without a container is nigh on impossible, but we'll disregard that fact and remain adamant that container needs to be a separate object. The second reasoning behind having it as a separate object is it helps promote object re-use which is a fundamental part to OOP, for example if we had another item that needed a container, we have an object ready for that use.

Looking at the code for the altered 'beverage' class you can see firstly the addition of 'extends container', this is inheritance at work, beverage now has inherited the public / protected (if there were any) properties and methods of the container class, so in this example, it has gained the 'getColour' and 'getContainer' methods. If any were protected, the child class (in this case 'beverage') could use the values internally, but the instantiated class could not.

The next addition is the expanded constructor, we introduce two new arguments, '$container' and '$colour', these are passed to the parent class's (container) constructor. If you look for where we actually create the beverage object, you will notice the new arguments passed, the types have now become an array consisting of the beverage type, the container type and the colour.

Now looking at the main loop of the program, you'll notice we're accessing the methods of the container class, namely 'getColour' and 'getContainer'. Pretty simple really.

Anything you don't understand, please ask. Feed back appreciated.
ap_
bread can i resume this and use it for a tutorials web?
Ed
QUOTE(ap__)
bread can i resume this and use it for a tutorials web?


Afraid not, you can however link to the post : http://www.zymic.com/forum/index.php?s=&am...ost&p=10761

You don't need to be registered to view it.
ap_
thnx!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.