PHP Object Oriented Programming

PreviousTable of ContentsNext
Understanding PHP SessionsUsing PHP with MySQL


Purchase and download the full PDF and ePub versions of this PHP eBook for only $8.99


PHP provides extensive support for developing object-oriented web applications. The area of object oriented programming is, however, large. Entire books can, and indeed have, been dedicated to the subject. As such, a detailed overview of object oriented software development is beyond the scope of PHP Essentials. Instead we will introduce the basic concepts involved in object oriented programming and then move on to explaining the concept as it relates to PHP development.

What is an Object?

An object is a self-contained piece of functionality that can be easily used, and re-used as the building blocks for a software application.

Objects consist of data variables and functions (called methods) that can be accessed and called on the object to perform tasks. These are collectively referred to as members.

What is a Class?

Much as a blueprint or architect's drawing defines what an item or a building will look like once it has been constructed, a class defines what an object will look like when it is created. It defines, for example, what the methods will do and what the member variables will be.


How is an Object Created from a Class?

The process of creating an object from the class 'blueprint' is called instantiation. Essentially, you instantiate an instance of the class and give that instance a name by which you will refer to it when accessing members and calling methods. You can create as many object instances of a class as you desire. Objects are instantiated using the new keyword. For example, to create an instance of a class called bankAccount we would write:

$accountObject = new bankAccount();

In the above example we now have an object called $accountObjhect of type bankAccount.

What is sub-classing?

It is possible to build classes that are derived from other classes, extending the functionality of the parent class to make it specific to a particular requirement. For example you might have a vehicle class which contains the attributes common to all vehicles, and a subclass called car which inherits all the generic vehicle attributes but adds some its own car specific methods and properties.

Defining a PHP Class

Before an object can be instantiated we first need to define the class 'blueprint' for the object. Classes are defined using the class keyword followed by braces which will be used to enclose the body of the class:

<?php
class bankAccount {
}
?>

We have now defined a class. The next step is add some functionality to the class.

PHP Class Constructors and Destructors

The next step in creating a class is to define what should happen when an object is first instantiated using the class, and also when that object is later destroyed. These actions are defined in the constructor and destructor methods of the class.

The constructor and destructor are really just functions that get called when the object is created and destroyed and are defined in the class body using the function keyword. This needs to be prefixed with the public qualifier. This means the method is accessible to code outside of the object. The default names for the constructor and destructor are __construct and __destruct respectively. Both methods can take arguments to be used in initializing the object. These are declared in the same way arguments are defined in any function (see PHP Functions for details).

We can now extend our bankAccount class to include a constructor and destructor:

<?php
class bankAccount {

   public function __construct($accountNumber, $accountName) {
         echo 'Object was just instantiated. Number = $accountNumber, Name = $accountName <br>';
   }

   public function __destruct() {
         echo 'Object was just destroyed <br>';
   }

}
?>

When the example is loaded into a browser we should get the following output:

Object was just instantiated. Number = 123456, Name = Gregory House
Object was just destroyed

Creating Members in a PHP Class

Class members are essentially variables and methods embedded into the class. Members can be public or private and static or variable.

public members can be accessed from outside the object. private members can only be accessed by methods contained in the class. This is the key to what is called data encapsulation. Object-oriented programming convention dictates that data should be encapsulated in the class and accessed and set only through the methods of the class (typically called getters and setters).

Members declared as static are immutable, in that once defined they cannot be changed (much like constants). Members and functions are prefixed with public, private and static when declared in the class. The default is public non-static.

We can now extend out bankAccount class to add member variables to hold the account name and number passed into the constructor. True to the concept of data encapsulation we will be creating methods to access these values later, so will mark them as private. We will also add to our constructor to assign the passed arguments to our new members. When doing so we need to use the $this variable to tell PHP we are setting variables in the current class:

<?php
class bankAccount {

   private $accountNumber;
   private $accountname;

   public function __construct($acctNumber, $acctName) {
        $this->accountNumber = $acctNumber;
        $this->accountname = $acctName;
   }

   public function __destruct() {
         echo 'Object was just destroyed <br>';
   }

}
        $myObj = new bankAccount('123456', 'Gregory House');
?>

The next task is to define methods that will give us access to our data.

Defining and Calling Methods

We define our own methods in much the same way as we declared the constructor and destructor methods with exception that we get to choose the names.

To demonstrate this we will add to our class to provide methods to get and set the account number and name:


<?php
class bankAccount {

   private $accountNumber;
   private $accountname;

   public function __construct($acctNumber, $acctName) {
        $this->accountNumber = $acctNumber;
        $this->accountname = $acctName;
   }

   public function __destruct() {
         echo 'Object was just destroyed <br>';
   }

   public function setAccountNumber($acctNumber)
   {
       $this->accountNumber = $acctNumber;
   }

   public function setAccountName($acctName)
   {
       $this->accountName = $acctName;
   }

   public function getAccountName()
   {
       return $this->accountName;
   }

   public function getAccountNumber()
   {
       return $this->accountNumber;
   }

}
?>

Now that we have defined our getter and setter methods to get and set the account values we can call the methods. This is done by specifying the name of the object on which the methods are being called. This is followed by '->', and then the name of the method we are calling (including the parentheses containing any arguments required):

<?php
class bankAccount {

   private $accountNumber;
   private $accountname;

   public function __construct($acctNumber, $acctName) {
        $this->accountNumber = $acctNumber;
        $this->accountname = $acctName;
   }

   public function __destruct() {
   }

   public function setAccountNumber($acctNumber)
   {
       $this->accountNumber = $acctNumber;
   }

   public function setAccountName($acctName)
   {
       $this->accountName = $acctName;
   }

   public function getAccountName()
   {
       return $this->accountName;
   }

   public function getAccountNumber()
   {
       return $this->accountNumber;
   }

}
        $myObj = new bankAccount('123456', 'Gregory House');

        $myObj->setAccountNumber('654321');

        $accountNumber = $myObj->getAccountNumber();

        echo "New Account Number is $accountNumber";

?>

The above example sets the account name and number when instantiating the object. It then calls the setAccountNumber() method of the object to change the account number, followed by a call to getAccountNumber (to verify the change), thereby producing the following output:

New Account Number is 654321

Subclassing in PHP

Once a class has been defined it is possible to create a new class derived from it that extends the functionality of the original class. The parent class is called the superclass and the child the subclass. The whole process is referred to as subclassing.

A subclass of a class can be defined using the extends keyword when declaring the subclass. For example we might choose to create a subclass of our bankAccount class called savingsAccount which defines an interest bearing type of account:

<?php
class savingsAccount extends bankAccount {

         private $interestRate = 5;

}

The important point to note here is that savingsAccount inherits all the members and methods of bankAccount and adds a new member (the interest rate).

We can extend the class further by adding a new method to return the interest rate:

<?php
class bankAccount {

   private $accountNumber;
   private $accountname;

   public function __construct($acctNumber, $acctName) {
        $this->accountNumber = $acctNumber;
        $this->accountname = $acctName;
   }

   public function __destruct() {
   }

   public function setAccountNumber($acctNumber)
   {
       $this->accountNumber = $acctNumber;
   }

   public function setAccountName($acctName)
   {
       $this->accountName = $acctName;
   }

   public function getAccountName()
   {
       return $this->accountName;
   }

   public function getAccountNumber()
   {
       return $this->accountNumber;
   }

}


class savingsAccount extends bankAccount {

   private $interestRate = 5;

   public function getInterestRate()
   {
       return $this->interestRate;
   }

}

        $mySaveObj = new savingsAccount('246810', 'Morgan Freeman');

        echo "Savings Account Number is " . $mySaveObj->getAccountNumber() . '<br>';

        echo "Interest Rate = " . $mySaveObj->getInterestRate() . '<br>';
?>

PHP Object Serialization

One of the interesting features of object oriented programming is the ability to take a snapshot of the current state of an object and then save that object to a file, or even transmit it over a network to another process where it will be re-activated. This concept is known in the object oriented world as object serialization.

All objects have built-in method called __sleep that is called before serialization. If you need your object to perform any housekeeping before being serialized you will need to override this method.

An object is serialized using the serialize() function and unserialized, not surprisingly, using the unserialize() function. As an example we can serialize our bankAccount object:

<?php
class bankAccount {

   private $accountNumber;
   private $accountname;

   public function __construct($acctNumber, $acctName) {
        $this->accountNumber = $acctNumber;
        $this->accountname = $acctName;
   }

   public function __destruct() {
   }

   public function setAccountNumber($acctNumber)
   {
       $this->accountNumber = $acctNumber;
   }

   public function setAccountName($acctName)
   {
       $this->accountName = $acctName;
   }

   public function getAccountName()
   {
       return $this->accountName;
   }

   public function getAccountNumber()
   {
       return $this->accountNumber;
   }

}

        $myObj = new bankAccount('246810', 'Morgan Freeman');

        $serialized = serialize ($myObj);

        echo 'Object is serialized<br>';

        $newObj = unserialize ($serialized);

        echo 'Object is unserialized<br>';

        print_r ($newObj);
?>

In the above example the object is serialized, then unserialized to a new object variable. We then use the print_r() function to verify the new object contains everything the old one did resulting in the following output:

Object is serialized
Object is unserialized
bankAccount Object ( [accountNumber:private] => 246810 [accountname:private] => Morgan Freeman )

Once we have the serialized data in our $serialized object we can do anything we want with it, such as write it to a file or send it through a network socket to another process where it can be unserialized and used.

Getting Information about a PHP Object

There are number of ways to find out information about classes.

An array of classes to which your script has access can be obtained using the get_declared_classes() function. The class_exists() function can be passed the name of a class to find out if such a class exists. A list of methods in a class can be obtained by passing the class name through to the get_class_methods() function.

It is also possible to find out if a class has a parent class using the get_parent_class() function which will either return the name of the parent class, or an empty string if no parent exists.

The method_exists() function, when passed an object pointer and method name as arguments, will return a true or false value indicating the existence of the method.

Purchase and download the full PDF and ePub versions of this PHP eBook for only $8.99