PHP Object Basics

This page provides a brief overview of objects in PHP. It is by no means comprehensive so if you need more information, please see the PHP documentation.

To begin with, the following is a simple address object. It has two member variables $name and $street. Both members have a visibility of private. There are three possible visibility values in PHP5.

If visibility is undefined, the default it public. Take a look at the class. Additional comments follow the code sample.

object01.php

Lines 10 - 13 defines the constructor for the class. With PHP, only one constructor can be defined per class. Notice that members of an object are accessed using the this keyword. Values are passed in when the object is created and then assigned to the member variables using this. Object methods and members are identified using "->" the arrow operator, not dots like Ruby or Java.

Lines 16-22 defines a display method to output the contents of the class. Notice the this keyword is used to access member variables.

Lines 24 - 38 define the standard getter and setter methods to access the member variables.

Lines 50-60 show the actual object in use. An object is created and immediately output. The name member is changed, and then the object is output again.

Full Address Object

The previous sample was only a partial address object. The following example is a complete Address object. Member variables have been added for city, state, and zip. In addition, a complete set of getters and setters has been added. The object is extended to demonstrate additional PHP object features.

object01b.php

 

PHP 5 Overloading

The previous example demonstrates how tedious it can be to code an object. Many IDE's exist to make the repetitive coding of getters and setters easier. In PHP 5, this sort of coding is made easier through the use of "overloading". Now in a language like Java, "overloading" is the ability to define multiple signatures for a single method. In PHP 5, overloading is accomplished through the use of special methods like __get, __set, and __call. The following example shows how the special __call method can be used to replace all the getters and setters from the previous example. (See the PHP documention for more on __get and __set).

object02.php

Essentially, if an object method is called and is not recognized, the method request is forwarded to the __call method. Lines 33-48 demonstrate code which performs all the functions of the previous getters and setters. When you run the script, you get the same output as you did before, with a lot less code.

Mutliple Constructor Signatures

Coming from a language like Java, where multiple signatures can be assigned to one constructor, The single constructor for PHP seems limiting. However, given PHP's dynamic nature, the same thing can be acheived by defining the method constructor arguments as an array. Then, different signatures can be passed and handled with the same method much like the __call method. The following is a simplistic example of this.

object03.php

Based on the number of arguments, the switch statement initializes the object accordingly. You could of course test of the kind of data passed, etc., and make the class definition much more complex.

Static Classes

Static classes are great for creating library functions when multiple object instances are not needed. The syntax and use of static classes is slightly different than regular objects.

The following example is a static class that encapsulates some of PHP's date functions. Comments follow the code.

object04.php

Notice the use of the static keyword to define class members and methods. Other than the static keyword, the class and member definitions look pretty much the same as before.

Look at lines 32 to 37 to see how the static class is used. Notice that a double colon (::) is used to call class methods. Also note that dollar sign ($) does not precede the class name.

Static classes can be great for saving a lot of typing or having to remember all the possible ways to use a particular function. The following example updates the date class with a few more date related methods.

object05.php

That concludes the PHP object basics.