#native_company# #native_desc#

Object Oriented Programming in PHP: The Way to Large PHP Projects Page 4

By Luis Argerich
on January 25, 2008

When an object of a derived class is created only its constructor is called, the constructor
of the Parent class is not called, this is a gotcha of PHP because constructor chaining is
a classic feature of OOP, if you want to call the base class constructor you have to
do it explicitely from the derived class constructor.It works because all methods of the
parent class are available at the derived class due to inheritance.


function Another() {


$this->Something();   //explicit call to base class constructor.



A nice mechanism in OOP is to use Abstract Classes, abstract classes are classes
that are not instanciable and has the only purpose to define an interface for its
derived classes. Designers often use Abstract classes to force programmers to
derive classes from certain base classes so they can be certain that the new
classes have some desired functionality. There’s no standard way to do that in
PHP but:
If you do need this feature define the base class and put a “die” call in its
constructor so you can be sure that the base class is not instanciable, now define
the methods (interface) putting “die” statements in each one, so if in a derived
class a programmer doesn’t override the method then an error raises.
Furthermore you might need to be sure since php has no types that some object is
from a class derived from you base class, then add a method in the base class
to identify the class (return “some id”), and verify this when you receive an
object as an argument. Of course this doesn’t work if the evil programmer
oveerides the method in the derived class but genrally the problem is dealing
with lazy programmers no evil ones!
Of course is better to keep the base class unreachable from the programmers,
just print the interface and make them work!
There’re no destructors in PHP.