Created
July 2, 2015 14:56
-
-
Save cmattoon/6f75cf41b776f6ea80c1 to your computer and use it in GitHub Desktop.
Late Static Binding in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo { | |
public static $attr = 'foo'; | |
public static function getAttr() { | |
return static::$attr; | |
} | |
public static function getAttr2() { | |
return self::$attr; | |
} | |
} | |
class Bar extends Foo { | |
public static $attr = 'bar'; | |
public static function getAttr3() { | |
return static::$attr; | |
} | |
public static function getAttr4() { | |
return self::$attr; | |
} | |
} | |
echo Foo::getAttr(); // 'foo' | |
echo Bar::getAttr(); // 'bar' | |
echo Foo::getAttr2(); // 'foo' | |
echo Bar::getAttr2(); // 'foo' | |
echo Bar::getAttr3(); // 'bar' | |
echo Bar::getAttr4(); // 'bar' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment