anjanesh wrote:In Java you can do that because the method signature includes its parameters.
In which language it isnt ? All major OOP languages have method overloading with parameters as part of its signature.
Its obvious that in PHP, parameters cant be treated as signatures because of weak type system.
I wouldn't call PHP a "major OOP language"

Perl would be another "language" which doesn't do overloading... as would JavaScript.
But I really cant understand why the number of parameters need to match the child's method too ! Isnt that the job of abtsract class ?
It basically boils down to conforming to an interface (whether that's by implementing an interface, or by extending a class).
If a method type-hints that it wants an instance of Foo as it's only parameter then it expects (read, requires) *any* subclass of Foo to also behave the same way.
Code: Select all
class Foo {
public function test(Bar $b) {
//
}
}
class MainClass {
public function doSomething(Foo $foo) {
$foo->test(new Bar());
}
}
$obj = new MainClass();
$obj->doSomething(new Foo());
Now if you were to change that interface by overriding the test() method with different parameters the type-hint would allow it; but it could potentially result in a fatal error:
Code: Select all
class SubFoo extends Foo {
public function test (Zip $zip) {
//whatever
}
}
$obj = new MainClass();
$obj->doSomething(new SubFoo());
//Error SubFoo::test() requires object of type Zip as parameter 1; object of type Bar given
You have similar problems if you add extra parameters which are not optional.
My argument is that PHP should check the types of the parameters and if they're compatible it should allow the override. It should on the same token check if any additional parameters have default values (and hence are "optional").