Background
The Zend Engine allows developers to declare constructor functions for their classes.
Classes which have a constructor function call this function on each newly-created
object, so it is suitable for any initialization that the object may need before it can be
used. In version 1.0, constructor functions are simply class methods that bare the same
name as the class itself. If a method exists that has the same name as the class it is
contained in – it’s automatically regarded as the constructor.
Need
Since it is very common to call parent constructors from derived classes, the way the
Zend Engine 1.0 works makes it a bit cumbersome to move classes around in a large
class hierarchy. If a class is moved to reside under a different parent, the constructor
name of that parent changes as well, and the code in the derived class that calls the parent
constructor has to be modified.
Overview
The Zend Engine 2.0 will introduce a standard way of declaring constructor functions –
by simply calling them by the name __construct().
Functionality
For example:
class Shape {
function __construct()
{
// shape initialization code
…
}
…
};
class Square extends Shape {
function __construct()
{
parent::__construct();
// square-specific initialization code
…
}
…
};
If we decide to introduce a new Rectangle class in between the Shape and Square class in
the hierarchy:
class Rectangle extends Shape {
function __construct()
{
parent::__construct();
// rectangle initialization code
}
…
};
We can do so without changing any code in the Square class, and only have to change the
class it is derived from:
class Square extends Rectangle {
…
};
Compatibility notes
For backwards compatibility, if the engine cannot find a __construct() function for a
given class, it will search for the old-style constructor function, by the name of the class.
Effectively, it means that the only case that would have compatibility issues is if the class
had a method named __construct() which was used for different semantics.
Dependencies of feature
No dependencies