mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-05 14:27:51 +02:00
Merge pull request #1544 from Glutnix/php-late-static-binding
[php/en] Adds late static binding
This commit is contained in:
@@ -712,6 +712,43 @@ use My\Namespace as SomeOtherNamespace;
|
|||||||
|
|
||||||
$cls = new SomeOtherNamespace\MyClass();
|
$cls = new SomeOtherNamespace\MyClass();
|
||||||
|
|
||||||
|
|
||||||
|
/**********************
|
||||||
|
* Late Static Binding
|
||||||
|
*
|
||||||
|
* /
|
||||||
|
|
||||||
|
class ParentClass {
|
||||||
|
public static function who() {
|
||||||
|
echo "I'm a " . __CLASS__ . "\n";
|
||||||
|
}
|
||||||
|
public static function test() {
|
||||||
|
// self references the class the method is defined within
|
||||||
|
self::who();
|
||||||
|
// static references the class the method was invoked on
|
||||||
|
static::who();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ParentClass::test();
|
||||||
|
/*
|
||||||
|
I'm a ParentClass
|
||||||
|
I'm a ParentClass
|
||||||
|
*/
|
||||||
|
|
||||||
|
class ChildClass extends ParentClass {
|
||||||
|
public static function who() {
|
||||||
|
echo "But I'm " . __CLASS__ . "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ChildClass::test();
|
||||||
|
/*
|
||||||
|
I'm a ParentClass
|
||||||
|
But I'm ChildClass
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
/**********************
|
/**********************
|
||||||
* Error Handling
|
* Error Handling
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user