Edit report at https://bugs.php.net/bug.php?id=61924&edit=1
ID: 61924 Updated by: col...@php.net Reported by: jenwelsh at yahoo dot com Summary: cannot use self in interface function declaration -Status: Feedback +Status: Not a bug Type: Bug Package: Class/Object related PHP Version: 5.4.1 Assigned To: laruence Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2012-05-04 02:18:34] col...@php.net 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. ------------------------------------------------------------------------ [2012-05-04 00:48:01] larue...@php.net Actually, I think it's a improvement of PHP-5.4, this change is introduced by fixing this issue: https://bugs.php.net/bug.php?id=60573 before this, you declare class A implements IComparable{ public function equals(self $other){ return ($this->var == $other->var) ? 'equal' : 'different'; } } actullay the self in here is A, not IComparable. but in the IComparable, the self means IComparable itsself. In scrupulously, A is_a Icomparable, but not equal to Icomperable, what do you think? thanks ------------------------------------------------------------------------ [2012-05-03 16:48:08] larue...@php.net Oh, sorry I misunderstanded , assign to my self , should have Sth to do with a fix made by me ------------------------------------------------------------------------ [2012-05-03 15:22:17] jenwelsh at yahoo dot com The reason I "think" it did work, is because it is **currently** working on a production site with PHP 5.3.11. And it **has** been working for over 2 years. ------------------------------------------------------------------------ [2012-05-03 14:52:08] larue...@php.net or you can change it to a feature request :) ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=61924 -- Edit this bug report at https://bugs.php.net/bug.php?id=61924&edit=1