Day 12 – Object Oriented Programming

Zend PHP Certification

This it the real meat and potatoes of PHP.  Classes contain methods (functions) and properties (variables).  This basic concept is also known as encapsulation or information hiding.

It’s important to note that when you create an instance of a class, passing it to functions or creating new variables do not make copies of the object, but instead create references to it.

class tempClass {
  1.   public $value;
  2. }
  3.  
  4. $test = new tempClass();
  5. $test->value = 5;
  6.  
  7. $new = $test;
  8. $new->value = 10;
  9.  
  10. echo $test->value; // Displays 10

Since I just showed you above, classes can contain properties as well as methods.  Here is an example of how to create and reference them.

class importantClass {
  1.   public $publicProperty;
  2.  
  3.   function basicFunction() {
  4.     echo "Function 1, ";
  5.     $this->otherFunction();
  6.   }
  7.  
  8.   function otherFunction() {
  9.     echo "Function 2, ";
  10.     echo $this->publicProperty;
  11.   }
  12. }
  13.  
  14. $instance = new importantClass();
  15. $instance->publicProperty = 5;
  16. $instance->basicFunction();
  17. // Displays: Function 1, Function 2, 5

Notice the use of $this.  You HAVE to use that to reference local variables and functions.  You can also use :: to reference the class name itself, usually this is only used when extending classes.

class a {
  1.   function display() {
  2.     echo "a::display()";
  3.   }
  4. }
  5. class b extends a {
  6.   function test() {
  7.     a::display();
  8.   }
  9.   function display() {
  10.     parent::display();
  11.   }
  12. }

Only because class b extends a can you reference the display method that way.  And since b extends a, calling parent::display() will call a::display().  A little confusing, I know.  But as an example, you can then create a class called c which extends b, and therefore can call a::display().

Now let’s say we want to assign some values or perform some functionality when creating or destroying an object, we can define those operations in the __construct and __destruct methods.

class box {
  1.   public $width;
  2.   public $height;
  3.  
  4.   function __construct($w, $h) {
  5.     $this->width = $w;
  6.     $this->height = $h;
  7.   }
  8. }
  9.  
  10. $square = new box(5, 5);
  11. echo $square->width; // Displays 5

But what if you wanted to prevent the user from accessing properties, calling functions, or even extending classes?  You could hide them with the visibility (a.k.a. PPP):

  1. Public: accessed from any scope
  2. Protected: accessed from within the class where it is defined and any descendants (extended classes).
  3. Private: accessed only from within the class where it is defined.
  4. Final: accessible from any scope but cannot be overridden in descendants.

Final can only be applied to methods and classes, a final class cannot be extended.  Here is an example:

class foo {
  1.   public $a;
  2.   protected $b;
  3.   private $c;
  4.  
  5.   function display() {
  6.     echo implode(',', array_keys(get_object_vars($this))));
  7.   }
  8. }
  9.  
  10. class bar extends foo {
  11.   function display() {
  12.     echo implode(',', array_keys(get_object_vars($this))));
  13.   }
  14. }
  15.  
  16. $test1 = new foo();
  17. $test1->display(); // Displays $a, $b, $c
  18.  
  19. $test2 = new foo();
  20. $test2->display(); // Displays $a, $b
  21.  
  22. echo implode(',', array_keys(get_object_vars($test1))));  // Displays $a

Note the use of a new function get_object_vars($obj) which returns an associative array of all the properties available in the object.

Static functions and properties are ones that can be called/referenced/saved without an instance.  These must be referenced with the :: syntax.  Here is an example of how they work:

class foo {
  1.   static $count = 0;
  2.  
  3.   function __construct() {
  4.     $this::count ++;
  5.   }
  6.   function __destruct() {
  7.     $this::count –;
  8.   }
  9. }
  10.  
  11. echo foo::count; // Displays 0
  12. $a = new foo();
  13. $b = new foo();
  14. $c = new foo();
  15.  
  16. echo foo::count; // Displays 3
  17. unset($a);
  18. unset($b);
  19.  
  20. echo foo::count; // Displays 1

And finally, there are constants.  Variables that are public, static, are declared with one value and cannot be changed.

Tomorrow I will be talking about Interfaces, Abstraction, Exceptions, and Reflection.  Happy New Year!

No Comments

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



  • Donate

    If my work has helped you and you want to return the favor, you could purchase something for me from my Amazon Wish List or send me a donation via PayPal.

  • License

    Unless otherwise noted, all source code and compiled files published on this website are released under the terms of the GNU Lesser General Public License.