Skip to content

Instantly share code, notes, and snippets.

@hirak
Last active December 14, 2015 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirak/5152651 to your computer and use it in GitHub Desktop.
Save hirak/5152651 to your computer and use it in GitHub Desktop.
PHP5.2で遅延静的束縛 ref: http://qiita.com/Hiraku/items/2f4e3c146857010b4ff6
<?php
function doSomething($hoge=__LINE__) {
var_dump($hoge);
}
<?php
class A {
static function hoge() {
var_dump(get_called_class());
}
}
class B extends A {}
class C extends B {}
A::hoge(); //A
B::hoge(); //B
C::hoge(); //C
//staticメソッドで、どのクラスのメソッドとして呼ばれたか判定が可能
<?php
class A {
static function hoge($class=__CLASS__) {
var_dump($class);
}
}
class B extends A {
//派生クラスには以下のコードをコピペする
static function hoge($class=__CLASS__) {
return parent::hoge($class);
}
}
class C extends B {
//派生クラスには以下のコードをコピペする
static function hoge($class=__CLASS__) {
return parent::hoge($class);
}
}
A::hoge(); //A
B::hoge(); //B
C::hoge(); //C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment