frederik holljen 
›
Things that should be fixed in PHP - Constructors
Constructors are a beautiful thing of object oriented programming. Unfortunately PHP has a serious design flaw with constructors and inheritance. Consider this class:
class Calc
{
private $value = null;
public function __construct( $initValue )
{
if( $initValue == 0 )
{
throw new Exception( "invalid init value" );
}
}
public function compute( $someOtherValue )
{
return $someOtherValue / $this->value;
}
}
The idea here is basically that the constructor performs its task of initializing the object making sure that the member variable $value can never be 0. This way the class is fail safe, it simply cannot go wrong. Or so you think..
So you give your nice Calc code to a friend so he can use it. However, he needs some more functionality so he extends the class
class AdvancedCalc extends Calc
{
public function __construct()
{
// some init of AdvancedCalc
}
public function advancedCompute()
{
// do stuff here...
$this->compute();
// do more stuff...
}
}
All of a sudden his code will crash, in YOUR code. Your friend will blame you, and you thought the code was fail safe! So what is going on here? In your friends code, extending your class he does not call the constructor of the parent class. Boof.. there goes your initializing code and your class will always fail. What is worse is that the error you will get is completely unrelated to any code you have written.
This case is pretty simple. Imagine however what it is like in big projects like eZ components where we write large classes that may be extended by others all the time. Without the constructors the objects of these classes will not be properly set up. Things will fail and the problem will seem to lie with eZ components.
The only way to give a proper warning about such a problem is to have a boolean in our classes that is set when the constructor is run. We can then check this boolean in all the methods. This is dirty hack however, and not a solution to the problem.
The real solution is of course to require that any existing constructors in your parent classes are run. This is common practice in other object oriented languages. Since you can't change this in PHP without breaking backwards compatibility I propose adding a keyword in front of a constructor to require it to be run. For example like this:
class RequiredConstructor
{
public required __construct()
{
}
}