OO in PHP5
New Object Oriented Features
The new object oriented features are too numerous to give a detailed description in this section. The object oriented language chapter goes over each feature in detail.
The following is a list of the main new features:
1. public/private/protected access modifiers for methods and properties
Allows the use of common OO access modifiers to control access to methods and properties.
class MyClass {
private $id = 18;
public function getId() {
return $this->id;
}
}
2. Unified constructor name __construct()
Instead of the constructor being the name of the class, it should now be declared as __construct(), making it easier to shift classes inside class hierarchies.
class MyClass {
function __construct() {
print "Inside constructor";
}
}
3. Object destructor support by defining a __destructor() method
Allows defining a destructor function that runs when an object is destroyed.
<?
class MyClass {
function __destruct() {
print "Destroying object";
}
}
?>
4. Interfaces
Gives the ability for a class to fulfill more than one is-a relationships. A class can inherit from one class only but may implement as many interfaces as it wants.
interface Display {
function display();
}
class Circle implements Display {
function display() {
print “Displaying circle\n”;
}
}
5. instanceof operator
Language level support for is-a relationship checking. The PHP 4 is_a() function is now deprecated.
if ($obj instance of Circle) {
print '$obj is a Circle';
}
6. final methods
The final keyword allows you to mark methods so that an inheriting class can’t overload them.
class MyClass {
final function getBaseClassName() {
return __CLASS__;
}
}
7. final classes
After declaring a class as final, it can’t be inherited. The following example would error out:
final class FinalClass {
}
class BogusClass extends FinalClass {
}
8. Explicit object cloning
In order to clone an object you have to use the clone keyword. You may declare a __clone() method which will be called during the clone process (after the properties have been copied from the original object).
class MyClass {
function __clone() {
print "Object is being cloned";
}
}
$obj = new MyClass();
clone $obj;
9. Class constants
Classes definitions can now include constant values, and are referenced using the class.
class MyClass {
const SUCCESS = "Success";
const FAILURE = "Failure";
}
print MyClass::SUCCESS;
10. Static members
Classes definitions can now include static members (properties), accessible via the class. Common usage of static members is in the Singleton pattern.
class Singleton {
static private $instance = NULL;
private function __construct() {
}
static public function getInstance() {
if (self::$instance == NULL) {
self::$instance = new Singleton();
}
return self::$instance;
}
}
11. Static methods
You can now define methods as static allowing them to be called from non-object context. Static methods don’t define the $this variable as they aren’t bound to any specific object.
<?
class MyClass {
static function helloWorld() {
print "Hello, world";
}
}
MyClass::helloWorld();
?>
12. abstract classes
A class may be declared as abstract so as to prevent it from being instantiated. However, you may inherit from an abstract class.
abstract class MyBaseClass {
function display() {
print "Default display routine being called";
}
}
13. abstract methods
A method may be declared as abstract, thereby deferring its definition to an inheriting class. A class that includes abstract methods must be declared as abstract.
abstract class MyBaseClass {
abstract function display();
}
14. Class type hints
Function declarations may include class type hints for their parameters. If the functions are called with an incorrect class type an error occurs.
function expectsMyClass(MyClass $obj) {
}
15. Support for dereferencing objects which are returned from methods.
In PHP 4, you could not directly dereference objects which are returned from methods. You would have to first assign the object to a dummy variable and then dereference it.
PHP 4:
$dummy = $obj->method();
$dummy->method2();
PHP 5:
$obj->method()->method2();
16. Iterators
PHP 5 allows both PHP classes and PHP extension classes to implement an Iterator interface. Once you implement this interface you will be able to iterate instances of the class by using the foreach() language construct.
$obj = new MyIteratorImplementation();
foreach ($obj as $value) {
print "$value";
}
For a more complete example, please refer to the “Advanced OOP & Design Patterns” chapter.
17. __autoload()
Many developers writing object-oriented applications create one PHP source file per-class definition. One of the biggest annoyances is having to write a long list of needed includes at the beginning of each script (one for each class). In PHP 5, this is no longer necessary. You may define an __autoload() function which is automatically called in case you are trying to use a class which hasn’t been defined yet. By calling this function the scripting engine is giving a last chance to load the class before PHP bails out with an error.
function __autoload($class_name) {
include_once($class_name . "php");
}
$obj = new MyClass1();
$obj2 = new MyClass2();
