Embarking on PHP5 Objects

After a brief few weeks studying Perl and its nuances I'm going to take a look at PHP5 Objects. Perl is rather strange when it comes to objects, its basically a hash with methods. May whatever god you Perl programmers worship bless you. Supposedly when Perl 6 comes out (similarly, we don't know when the end of the world will come either) it will have real objects.

One of the annoying things with object in PHP 4 was you had to use a lot of references, you know, that funny & symbol. No longer needed in PHP 5 because you use "Object Handles" perhaps similar to a file handler you when fopen a file. Also available now are access modifiers "public/protected/package" and interface implementation. I can hear the beer mugs of java programmers being raised in celebration to this one. Also new to PHP 5 are real constructors and destroy methods. There are many more features, but lets see some code.

I'm reading this FREE book "Power PHP 5 Programming" as I work out some examples and familiarize myself with PHP5. Download all 720 pages as pdf.

This was my first foray into PHP5:

class Person {
    private $name_first;
    private $name_last;

    function __construct($first, $last) {
        $this->name_first = $first;
        $this->name_last  = $last;
    }

    public function get_name() {
        return sprintf("%s %s", $this->name_first, $this->name_last);
    }

    private function cant_touch_this() {
        return "haha you can't touch me!";
    }

}

Simple enough. The specially named constructor sets the values passed to the private members.

I instantiated the class and did this:

$p = new Person("Nola", "Stowe");
var_dump($p);
print $p->name_first;

And the output:

object(Person)#1 (2) {
  ["name_first:private"]=>
  string(4) "Nola"
  ["name_last:private"]=>
  string(5) "Stowe"
}

Fatal error: EEIIDIOT!!! You cannot access private property Person::$name_first in 
D:\server\xampp\htdocs\php5\test.php on line 33. GOSH what are you thinking?!!?

What? PHP 5 was written by Napoleon Dynamite??

Just kidding. It actually says:

Fatal error: Cannot access private property Person::$name_first in 
D:\server\xampp\htdocs\php5\test.php on line 33

From the dump I can see the properties are marked private and their values. I try to access the first name from outside the class. Fatal Error.

I defined a public method. Let see how that performs:

$p = new Person("Nola", "Stowe");
print $p->get_name();

And the output:

Nola Stowe

Great. Now lets try to access the private method:

$p = new Person("Nola", "Stowe");
print $p->cant_touch_this();

Output:

Fatal error: Call to private method Person::cant_touch_this() from context '' 
in D:\server\xampp\htdocs\php5\test.php on line 34

Another new feature is in PHP5 is interfaces. From my Java days in college I don't remember really understanding interfaces very well. Course, now I'm much wiser and I think I got it. Here's an example (albeit maybe not a good one)

class Data_Record {
    private $data;

    function __construct($data) {
        $this->data = $data;
    }

    function save() {
    }

}

class Contact_Record extends Data_Record
                    implements Storable {

    function __construct($first, $last) {
        $this->data['first_name'] = $first;
        $this->data['last_name']  = $last;
    }

    function save() {
        $fh = fopen('contact.txt', 'w');
        fwrite($fh, $this->convert_to_stored($this->data));
        fclose($fh);
    }

}

A simple class Data_Record with a single private variable $data. The Contact record extends from there, sets values in the constructor and saves the data to a file. Notice the "implements Storable" in the class definition. Here's the Storable class, which is empty to defer the implementation to those who use it:

interface Storable {

    function convert_to_stored();

}

The Storable interface has only one method - blank. To use the interface, you must implement in your class Contact_Record class:

    function convert_to_stored() {
        return serialize($this->data);
    }

Since you can only extend from one class, interfaces allow you to impose a set of required-ness on various parts of your program The Contact_Person knows how to convert its data to a version that can be stored to disk, you may also have other unrelated objects such as Permissions, Groups etc that you would also like to be able to call convert_to_stored. Otherwise you would have to impose a convert_to_stored method in each of Contact_Person, Permission, Group etc classes instead of just directing the class to implement the Storable interface.

That's just a few of the goodies in PHP5. Will the new object model help PHP? I don't know. With all this Ruby talk about Ruby taking over Java, I wonder what that means for PHP5.