Skip to content

Instantly share code, notes, and snippets.

@mugyu
Created March 11, 2019 09:36
Show Gist options
  • Save mugyu/6d929a4c1ac2a85d19eaf546fc032d1b to your computer and use it in GitHub Desktop.
Save mugyu/6d929a4c1ac2a85d19eaf546fc032d1b to your computer and use it in GitHub Desktop.
比較 self::hoge() and static::hoge()
<?php
/**
* 比較 self::hoge() and static::hoge()
*/
class A1 {
public static function message() {
return "A1\n";
}
public function say() {
// self::だと、そのレベルのクラスのメソッドを束縛する
echo self::message();
}
}
class B1 extends A1 {
public static function message() {
return "B1\n";
}
}
class A2 {
public static function message() {
return "A1\n";
}
public function say() {
// static::だと、最終レベルのクラスのメソッドを束縛する
echo static::message();
}
}
class B2 extends A2 {
public static function message() {
return "B1\n";
}
}
$b1 = new B1();
$b1->say(); // => A1
$b2 = new B2();
$b2->say(); // => B1
// 最終レベルで self:: を書いているなら特に問題ないけど、
// 中間レベルのクラスで書くと勘違いしそうな感じ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment