note 106289 added to language.oop5.late-static-bindings
| From: | fchoquet at NOSPAM dot example dot com | Date: | Tue, 25 Oct 2011 13:36:57 +0000 |
| Subject: | note 106289 added to language.oop5.late-static-bindings | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-184119@lists.php.net to get a copy of this message | ||
A tricky behaviour of LSB:
If you forget a "static" keywork in the method definition you can get very unexpected
results.
<?php
class BaseClass {
public function callTest() { // static accidentally omitted
static::test();
}
protected static function test() {
echo 'BaseClass::test<br />';
}
}
class ExtendedClass extends BaseClass {
protected static function test() {
echo 'ExtendedClass::test<br />';
}
}
class UserClass {
public function useExtendedClass() {
ExtendedClass::callTest(); // Static call of the *acidentally* non static method
}
public function test() {
echo 'UserClass::test<br />';
}
}
BaseClass::callTest(); // BaseClass::test, as expected
ExtendedClass::callTest(); // ExtendedClass::test, as expected
$userClass = new UserClass();
$userClass->useExtendedClass(); // ExtendedClass::test is expected, but displays
UserClass::test!!!
?>
----
Server IP: 69.147.83.197
Probable Submitter: 82.226.203.156
----
Manual Page -- http://www.php.net/manual/en/language.oop5.late-static-bindings.php
Edit -- https://master.php.net/note/edit/106289
Del: integrated -- https://master.php.net/note/delete/106289/integrated
Del: useless -- https://master.php.net/note/delete/106289/useless
Del: bad code -- https://master.php.net/note/delete/106289/bad+code
Del: spam -- https://master.php.net/note/delete/106289/spam
Del: non-english -- https://master.php.net/note/delete/106289/non-english
Del: in docs -- https://master.php.net/note/delete/106289/in+docs
Del: other reasons-- https://master.php.net/note/delete/106289
Reject -- https://master.php.net/note/reject/106289
Search -- https://master.php.net/manage/user-notes.php