Skip to content

Instantly share code, notes, and snippets.

@monmonmon
Last active December 25, 2015 06:49
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 monmonmon/6934596 to your computer and use it in GitHub Desktop.
Save monmonmon/6934596 to your computer and use it in GitHub Desktop.
静的メソッドの mixin(mixin.php から抜粋)
<?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