Bug #61924 [Fbk]: cannot use self in interface function declaration
| From: | colder@php.net | Date: | Fri, 04 May 2012 02:18:34 +0000 |
| Subject: | Bug #61924 [Fbk]: cannot use self in interface function declaration | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-170148@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=61924&edit=1
ID: 61924
Updated by: colder@php.net
Reported by: jenwelsh at yahoo dot com
Summary: cannot use self in interface function declaration
Status: Feedback
Type: Bug
Package: Class/Object related
PHP Version: 5.4.1
Assigned To: laruence
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2012-05-04 00:48:01] laruence@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] laruence@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] laruence@php.net
or you can change it to a feature request :)
------------------------------------------------------------------------
[2012-05-03 14:45:32] laruence@php.net
it's always not allowed since php 5.2, why are you thinking it worked once?
------------------------------------------------------------------------
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