Edit report at https://bugs.php.net/bug.php?id=61924&edit=1
ID: 61924
User updated by: jenwelsh at yahoo dot com
Reported by: jenwelsh at yahoo dot com
Summary: cannot use self in interface function declaration
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:
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.
Previous Comments:
------------------------------------------------------------------------
[2012-05-04 02:24:55] [email protected]
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.
------------------------------------------------------------------------
[2012-05-04 02:18:34] [email protected]
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] [email protected]
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] [email protected]
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.
------------------------------------------------------------------------
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