Last active
December 25, 2015 06:49
-
-
Save monmonmon/6934596 to your computer and use it in GitHub Desktop.
静的メソッドの mixin(mixin.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
<?php | |
// mixin クラス | |
class MixinClass | |
{ | |
// 静的メソッド | |
public static function mixin_static_method($message, DateTime $date) | |
{ | |
print "これは MixinClass の静的メソッドです $message ".$date->format('Y-m-d')."\n"; | |
} | |
} | |
// mixin 対象クラス | |
class Foo | |
{ | |
// __callStatic (php5.3.0 以降) をオーバーライドして、静的メソッドの mixin を実装します | |
// 静的メソッド $method_name がこのクラスでも先祖クラスでも未定義な場合、ここへ到達。 | |
public static function __callStatic($method_name, $args) | |
{ | |
if (is_callable("MixinClass::$method_name")) { | |
// MixinClass::$method_name を実行 | |
return call_user_func_array("MixinClass::$method_name", $args); | |
} else { | |
// そんな静的メソッドはどこにも見つかりませんでした。。。 | |
throw new BadMethodCallException(); | |
} | |
} | |
} | |
print "// Foo::mixin_static_method\n"; | |
Foo::mixin_static_method("yahoooooo!", new DateTime()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment