LoginSignup
0
0

More than 5 years have passed since last update.

PHP 遅延静的束縛 (Late Static Bindings) static:: 分かりやすいサンプル

Last updated at Posted at 2019-04-28

class Animal {
    protected static $name = 'Animal';

    public static function getName() {
        return static::$name;
    }
}

class Cat extends Animal {
    protected static $name = 'Cat';
}

echo Animal::getName(); // Animal
echo Cat::getName(); // Cat


class C
{
  public static function foo() {
    static::bar(); // bar()のスコープは直近の "非転送コール" のクラス(C::foo()ならC。CC::foo()ならCC)
  }

  public static function bar() {
    echo 'C_bar' . PHP_EOL;
  }
}

class CC extends C
{
  public static function bar() {
    echo 'CC_bar' . PHP_EOL;
  }
}

C::foo(); //C_bar
CC::foo(); //CC_bar  

参考サイト
https://maeharin.hatenablog.com/entry/20130202/php_late_static_bindings

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0