Object-Oriented Features in PHP 5.0

An object is an encapsulation of data and methods. The data goes by various names - attributes, fields, properties. And the methods have various names - methods, functions, operations. A class is the definition of an object. It is the template used to create objects. Objects are instances of a class. Object oriented programming is built around 4 principles:

Class Definition

The class definition starts with the keyword class. The definition consists of a list of properties and methods. The methods consists of constructors, destructor, accessors, and mutators. The access rights to the properties and methods are specified by the keywords - public, protected, and private. Any class can access public attributes and methods. Only classes that inherit from a class can access protected attributes and methods. And only methods within the same class can access its private attributes and methods. An object is instantiated using the keyword new.

class MyClass
{
  // List of attributes

  // Constructors

  // Destructor

  // Accessors

  // Mutators

  // Other Methods

}

MyClass myObj = new MyClass();

Constructors and Destructors

In most object-oriented languages constructors have the same name as the class and are declared public. Constructors can be overloaded, i.e. you can use the same name but different parameter lists. In PHP 5.0 the constructor is the function _construct(). In a constructor you would normally initialize attribute values. The _destruct() function is not required but you could write code there that would clean up after the object was destroyed like closing files or database connections.

Accessors and Mutators

If attributes have been declared to be private then accessors and mutators have to be used. Accessors are functions that return the values of attributes and Mutators are used to change or set their values. For public attributes accessors and mutators are redundant.

class Person
{
  private $name;

  // Constructor
  function _construct ($aName)
  {
    $this->name = $aName;
  }

  // Accessor
  function getName ()
  {
    return $this->name;
  }

  // Mutator
  function setName ($aName)
  {
    $this->name = $aName;
  }
}

$disneyCharacter = new Person ("Mickey Mouse");

print $disneyCharacter->getName();

$disneyCharacter->setName ("Donald Duck");
$this is a variable that holds the reference (or address) of the object that has been created using this class. $this is a way of referring to the object thus created internally in the class definition.

static Attributes and Methods

The meaning of the keyword static depends on the context in which it is used. static used as a modifier for an attribute means that there is only one copy of that attribute for the class. Objects of that class do not have separate copies of that attribute and all objects can access that one copy of the attribute. static attributes can be used to share common data among objects. static used as a modifier for a method means that one can access the method without having to first having to create an object of that class. A static method is referred to using the class name rather than the name of an object.

class MyClass
{
  private static $numObjects = 0;

  function _construct ()
  {
    self::numObjects++;
  }
   
  static function printNumObjects ()
  {
    print self::numObjects;
  }
}

MyClass::printNumObjects();

Class Constants

Classes can have constant values using the keyword const and are referenced using the class name.

class MyClass
{
  const TAXRATE = 0.0825;
}
print MyClass::TAXRATE;

Cloning

PHP 5.0 provides a method called clone that creates a copy of an object.

$newObject = clone $oldObject;
However, references are copied as references, i.e. the addresses are copied. If you want a deep copy of all the attributes and all the references then you can provide a _clone() function that defines how the cloning will be performed. This function will override the generic clone() function when clone is called.

Inheritance

To denote that a class inherits from another the keyword extends is used. PHP 5.0 allows only single line of inheritance. A child can have only one parent but a parent can have many children. A child inherits all the attributes and all the methods of its parent. It has access only to those attributes and methods that are marked public or protected. Inheritance relationship is transitive: if A inherits from B and B inherits from C, then A inherits from C. To check if there is an inheritance relationship between two classes X and Y juxtapose the phrase is-a ("X is a Y" or "Y is a X") between them. If one of those phrases make logical sense then there is an inheritance relationship between them.

Keywords parent:: and self::

The keyword parent refers to the immediate parent class of a child. One can then access the attributes and methods of the parent class using this keyword.

class Ancestor
{
  private $name;

  function _construct ($aName)
  {
    $this->name = $aName;
  }
}

class Descendant extends Ancestor
{
  private $phone;

  function _construct ($aName, $aPhone)
  {
    parent::_construct ($aName);
    $this->phone = $aPhone;
  }
}
The keyword self is used to refer to static attributes and methods within the class instead of using the $this reference variable.

Operator instanceof

The instanceof operator returns true if an object is an instance of the given class.


if ($obj instanceof MyClass)
{
  // Do something
}
Even though PHP is not a strongly typed language you can specify the type for an object (though not for a primitive type).
public function someFunction (MyClass $obj)
{

}

final Methods and Classes

A final method cannot be overridden or redefined in a child or descendant class. A final class cannot be extended.

abstract Methods and Classes

An abstract method has just the method name and the parameter list but not the definition or the body of the method. An abstract class has at least one abstract method. You cannot create an object of an abstract class. However the abstract class provides the template for the hierarchy of classes that descend (or inherit) from it.

Interfaces

An interface is a list of methods (method name and parameter list) but not the definition or body. The interface defines the functionality that is needed. A class can implement one or more interfaces.

interface MyInterface
{
  function method1();
  function method2();
}

class MyClass implements MyInterface
{
  function method1()
  {
    // definition of method1
  }

  function method2()
  {
    // definition of method2
  }
}

Polymorphism

Polymorphism means many forms. If we want to give a literal interpretation of this concept we can say that an object can come in many forms or types. We can achieve polymorphism in two ways - through inheritance and through interfaces. Polymorphism allows us to have a plug-and-play ability as for the use of objects is concerned.

Polymorphism through inheritance: For an object of a certain class we can always use objects of classes that inherit or are descended from it.

Polymorphism through interfaces: We can always replace an object of a class that implements an interface with object of any other class that implement the same interface.