Skip to content

Instantly share code, notes, and snippets.

@cmattoon
Created July 2, 2015 14:56
Show Gist options
  • Save cmattoon/6f75cf41b776f6ea80c1 to your computer and use it in GitHub Desktop.
Save cmattoon/6f75cf41b776f6ea80c1 to your computer and use it in GitHub Desktop.
Late Static Binding in PHP
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