|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-12-20 10:56 UTC] jpauli@php.net
Description:
------------
"self" should be thought as "the class where the keyword is written in".
Here is then a strange behavior using inheritance :
Test script:
---------------
class Foo {
public function setSelf(self $s) { }
}
class Bar extends Foo {
public function setSelf(self $s) { }
}
Expected result:
----------------
Strict Standards as Bar::setself() signature doesn't respect Foo::setSelf()
signature.
Foo's one has a type hint on Foo (using self keyword), but Bar's one on Bar
(still using self keyword) : the methods then have a signature mismatch, and PHP
should complain about that
Actual result:
--------------
Nothing happens.
Trying this gives the correct E_STRICT error, correct behavior :
class Foo {
public function setSelf(Foo $s) { }
}
class Bar extends Foo {
public function setSelf(Bar $s) { }
}
Patchesbug60573.phpt (last revision 2011-12-20 16:01 UTC by laruence@php.net)bug60573.patch (last revision 2011-12-20 16:01 UTC by laruence@php.net) Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 22:00:01 2025 UTC |
This is really a tough one, think about following: <?php class Foo { public function setSelf(self $s) { } } class Bar extends Foo { public function setSelf(parent $s) { } } ?> --------------------- <?php class Foo { public function setSelf(Foo $s) { } } class Bar extends Foo { public function setSelf(parent $s) { } } ----------------------- <?php class Base { } class Foo extends Base{ public function setSelf(parent $s) { } } class Bar extends Foo { public function setSelf(Base $s) { } } and more.......This patch seems to cause strange behaviour for interfaces. If the "self" keyword is defined in an interface, I would think that "self" would refer to the implementing class. Test script which worked in 5.3.19: <?php interface Comparable { function compare(self $compare); } // Which is then implemented: class Foo implements Comparable { function compare(self $compare) {} } class Bar implements Comparable { function compare(self $compare) {} } $foo = new Foo(); $bar = new Bar(); $foo->compare($foo); $bar->compare($bar);