PHP

The Basics of Objects in PHP

I declare classes in PHP by using the class keyword. The contents of the class (the data members and implementation methods) are enclosed between brackets.

<?php
  class Product
  {
    // contents, including properties and methods, go here
  }
?>

For the previous example, I said that the properties are the product identification number (PID), product name, description, price, and location. In PHP, you declare these properties in your class as follows:

<?php
  class Product
  {
    public $id;
    public $name;
    public $desc;
    public $price_per_unit;
    public $location;
  }
?>

The class Product now has five properties, or member variables, that represent its fundamental information. Like all variables in PHP, they have no declared type associated with them. What is new about the code I have written, however, is that I have added the keyword public in front of each line. This keyword tells PHP that everybody is allowed to inspect and modify the data that I have assigned to this object.

When you declare a member variable in PHP, you can optionally provide it with a default or "initial" value by assignment:

<?php
  class Product
  {
    public $id;
    public $name = "Unknown Product";
    public desc;
    public $price_per_unit;
    public $location = LOCAL_PRODUCT;
  }
?>

Each new instance of the class has the $name and $location member variables assigned to those default values. The other member variables are unset until a value is assigned to them.

In order to create an object instance, you use the new keyword along with the name of the class you wish to instantiate:

<?php
  $prod = new Product();
?>

You can access a member variable on an instance of the class by using the -> operator.

<?php
  echo "The product's name is: {$prod->name}<br/>\n";
?>

Similarly, you can set the value of a member variable with the same operator:

<?php
  $prod->name = "StormMeister 3000 Weather Radar";
?>

To add a member function to your class, simply put the function inside of the class declaration alongside the member variables. The function now has a visibility level:

<?php
  class Product
  {
    public $id;
    public $name;
    public $desc;
    public $price_per_unit;
    public $location;
    public function get_number_in_stock()
    {
       if ($this->location == LOCAL_PRODUCT)
       {
         // go to local database and find out how many I
         // have, returning this number, 0 if none.
       }
       else if ($this->location == NAVIGATION_PARTNER_PRODUCT)
       {
         // communicate with my navigation partner's systems
         // and ask how many are left.
       }
       else
       {
         // this is an error -- I'll talk about how to deal with
         // this more elegantly later.
         return -1;
       }
     }
   }
?>

Like regular functions in PHP, you cannot declare more than one member function with the same name within a class.

To access a member variable from within a member function, you must use the special variable (also called a pseudo-variable) $this. It refers to the current instance of the object itself. You combine it with the -> operator to access the member variables.

if ($this->price_per_unit > 100)
{
  echo "Phew! I'm expensive";
}
else
{
  echo "I'm affordable";
}

To call a member function given a reference to an object instance, you use the -> operator:

<?php
  $prod = get_most_popular_product();
  $num_stocked = $prod->get_number_in_stock();
?>

This example shows us more clearly that the method get_number_in_stock is not only operating on a broad class of products, but also on a specific instance of a product. I are now working in a system where the inherent properties and capabilities of items are what I use. I can now consider my program execution to be something like that in Figure 2.

Figure 2. Object-oriented classes have both data and implementation.

To call a member function from within one of my classes, I must again prefix the reference with $this: pseudo variable, as follows:

<?php
  class Product
  {
    // etc....
    public function do_i_have_any()
    {
      if ($this->get_number_in_stock() > 0)
        return "YES";
      else
        return "NOPE";
    }
  }
?>

Once I have created and used an object in PHP, I can get rid of it by simply removing all references to it. The PHP language engine figures out it is no longer in use and cleans it up.

<?php
  $prod1 = new Product();
  $prod2 = $prod1;         // both are pointing to the same obj
  $prod1 = NULL;           // object not cleaned up yet: $prod2
  $prod2 = NULL;           // no more references --
                           // object will be deleted
?>

PHP will not always delete the object immediately; it will only delete the object when it is sure that nobody is referencing it any longer. (All objects are eventually destroyed when script execution terminates.) If you want to make sure some code is executed when you are done with the object, you might want to consider adding a clean_up method or something similar to call when you are certain you are finished with it.

by BrainBellupdated
Advertisement: