This week I learned something new about PHP5 interfaces.
I had always assumed that interfaces were handled very strictly in PHP5.
While browsing the code of the Zend Framework form component, I noticed that form elements called the validators' isValid method when validating data. Nothing unusual so far. The Zend validators implement the Zend_Validate_Interface interface. This specifies exactly three methods that must be implemented, including the "isValid" method used in the forms.
This method has the following signature:
public function isValid($value);
As you can see, exactly one parameter is passed. Passing a second parameter would lead to a "Fatal Error," since the interface was defined differently.
But what surprised me was that the form validation simply passed two parameters to isValid. After writing a small test program, I had to conclude that PHP is not strict about parameters at all. As soon as additional parameters follow and have default values, PHP ignores them.
Example:
public function isValid($value, $foo = null, $bar = 123);
The PHP core developers apparently built this crutch because PHP does not support polymorphism. This is bad because it means you cannot rely on interfaces 100%. The basic parameters are present, but subclasses can receive additional parameters if needed.
In my view, this really is inconsistent. But that is certainly open to debate. The same goes for the workarounds for emulating polymorphism using the PHP function "func_get_args".
Let's hope for PHP 5.3 — perhaps something will still change here.