PHP DevCenter
oreilly.comSafari Books Online.Conferences.


Introduction to PHP Objects, Part 2
Pages: 1, 2

Overloading Member Functions and Parent Referencing

One of the most powerful features of object-oriented programming is the ability to overload a function defined within another class. For instance, adding the following to the class definition of car:

function getID() {
  return "No ID Found";

Any references to the member function getID() from an instance of the car class will result in executing the function defined within the car class. Although useful, there may be times when the functionality of both the initiated object and its parent class is desired (for instance, when the parent class provides a basic functionality to be extended by a child class). To facilitate this, PHP provides the parent reference. Below is an example of the parent reference in action:

Also in PHP Foundations:

Using MySQL from PHP, Part 2

Using MySQL from PHP

MySQL Crash Course, Part 3

MySQL Crash Course, Part 2

MySQL Crash Course

class parentclass {
  function foo() {
   echo "This is the parent class<BR>";
 class childclass extends parentclass {
  function foo() {
   echo "This is the child class<BR>";
 $myclass = new childclass();

Executing this script produces the following output:

 This is the child class
 This is the parent class

Object Serialization

Along with the active functional aspects of objects in PHP, they can also be stored (in their current state) in a database or a file system through a process called serialization. This process is accomplished through the functions serialize() and unserialize(), which converts an instance of an object (or any variable) into a string that can be written to a file or stored within a database, as shown in the code below:

  class foo {
   var $a = 1;
   function getvar() {
    return $this->a;
   function addvar() {
  $bar = new foo();
  echo "The value of the variable a is: ".$bar->getvar()."<BR>";
$str = serialize($bar);
  $foobar = unserialize($str);
  echo "The value of the variable a is: ".$foobar->getvar()."<BR>";

As the above code demonstrates, first we define and create an instance of the class foo (which we store in the variable $bar) and echo its variable to the browser. Then, we increment the variable in our $bar class and store the serialization string of $bar created by the serialize() function in the $str variable. At this point, note that the instance $bar has been completely destroyed. From this point, we recreate the object from its serialization string and store it into a new variable $foobar, effectively recreating the object from its original state before it was destroyed. Here's what the output will look like:

 The value of the variable a is: 1
 The value of the variable a is: 2

Please note that, when an object is being recreated through a call to the unserialize() function, it is important that the class defining the instance of the object exist. For example, attempting to reconstruct the foo class above, without having the class definition for the foo class, results in an empty, useless class being created.

More to Come

This concludes my discussion (at least for now) of using objects within PHP! I hope you now have a firm handle on how objects are handled within PHP, and that you also have some great tricks at your disposal to create reusable and storable PHP code. In my next article, I'll be tackling a whole new aspect of writing scripts in PHP when I discuss the closer thing that PHP has to a C-style pointer: References.

John Coggeshall is a a PHP consultant and author who started losing sleep over PHP around five years ago.

Read more PHP Foundations columns.

Return to the PHP DevCenter.

Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!

Sponsored by: