From 6c14f507c24524de4815cac05b55bf9a56e7cd06 Mon Sep 17 00:00:00 2001 From: Brett Taylor Date: Fri, 16 Oct 2015 12:04:17 +1300 Subject: Adds late static binding to PHP reference --- php.html.markdown | 49 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/php.html.markdown b/php.html.markdown index 39ec5aef..e64f7bfe 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -693,6 +693,43 @@ use My\Namespace as SomeOtherNamespace; $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 * @@ -708,9 +745,9 @@ try { // When using try catch blocks in a namespaced enviroment use the following -try { +try { // Do something -} catch (\Exception $e) { +} catch (\Exception $e) { // Handle exception } @@ -719,13 +756,13 @@ try { class MyException extends Exception {} try { - - $condition = true; - + + $condition = true; + if ($condition) { throw new MyException('Something just happend'); } - + } catch (MyException $e) { // Handle my exception } -- cgit v1.2.3