|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-05-03 14:20 UTC] jenwelsh at yahoo dot com
Description:
------------
I am reviewing existing code on a PHP5.4.1 testbed. I've discovered that interface declarations using 'self' as a type hint no longer allow implementations to use 'self' but require them to use the interface name.
It is no longer possible for an interface to declare a method that requires the implementor's class as a typehint without declaring that class specifically. And that would limit the usefulness of that interface to one class only.
Test script:
---------------
interface IComparable {
public function equals(self $other);
}
class A implements IComparable{
protected $var;
public function __construct(self $v){
$this->var=$v;
}
public function equals($other){
return ($this->var == $other->var) ? 'equal' : 'different';
}
}
$a1= new A(7);
$a2= new A(5);
$a3= new A(5);
echo $a1->equals($a2),"\n";
echo $a2->equals($a3),"\n";
Expected result:
----------------
different
equal
Actual result:
--------------
PHP Fatal error: Declaration of A::equals() must be compatible with IComparable::equals(IComparable $other)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 05:00:01 2025 UTC |
The rule was previously accepted as the type hints where simple syntactic check (basically string comparisons). This was wrong, and got fixed in 5.4. You cannot use self/parent as type hints as they depend on the current type in a covariant fashion, and type hints need to be contravariant. To explicit the problem in 5.3: interface A { public function foo(self $a); } class B implements A { public function foo(self $a) { } } class C implements A { public function foo(self $a) { } } If B (and C) are valid (per <php5.4 rules) implementations of A, it means that anything you are allowed to do on A should work on B (and C) (by Liskov's substitution principle) now let's see: function test(A $a) { $a->foo(new C); } test(new B) will fail test(new C) will work A side effect from this fix is that the wrong typehint now fails with an error, since B/C are invalid implementations of A.Just to make it clear, you can use self/parent as type hints, as long as the class they reference is right, for instance: class A { public function foo(self $plop) { } } class B extends A { public function foo(A $plop) { } } class C extends A { public function foo(parent $plop) { } } are all equally fine. since parent in C is A, and self in A is A.I am in the unfortunate situation of agreeing that you are technically correct. But I must say that it makes using "self" as a typehint fairly useless in an interface. Now if I want to require that a method argument be same type as $this, I'll have to do it this way: interface IComparable { public function equals($other); } class A implements IComparable{ protected $var; function equals($other) { if(get_class($other)!== get_class($this)) throw Exception('wrong arg class'); return $this->var==$other->var; } } And that will make the interface much less specific and strong.You are confusing "specific and strong" with wrong. With self as a type hint, you make the type system unsound, which means that it will provide you with absolutely no (virtual) guarantees. For example: function isEqual(IComparable $a, IComparable $b) { return $a->equals($b); } even though the IComparable interface declares a equals function that takes a IComparable, this code is not valid if self typehints work the way you want. Another design problem of your example is: equals should be reflective, that is: $a->equals($b) should mean $b->equals($a); This fails to be the case if self typehints are possible: class A extends IComparable { def equals(self $a) { return true; } } class B extends A { def equals(self $a) { return true; } } $a = new A; $b = new B; $a->equals($b); // OK $b->equals($a); // type error