PHP run static method before each static function -
i want able call function automatically before function invoked. problem __callstatic runs if method not exist.
see code below.
i want make always_run() run before function called in static class.
class test { public static function __callstatic($method, $parameters){ echo __class__ . "::" . $method; if (method_exists(__class__, $method)) { self::always_run(); forward_static_call_array(array(__class__,$method),$args); } } public static function always_run() { echo "always_run"; } public static function my_func() { echo "myfunc called"; } } test::my_func(); // output: always_run myfunc wascalled
creating static classes such have global state bad design. should creating object, can run sort of set code need in constructor.
class test { public function __construct() { // code run once when object constructed. } } static state makes such classes difficult test , maintain. static classes cannot mocked, code depends on class cannot independently tested.
here's article out: https://r.je/static-methods-bad-practice.html
Comments
Post a Comment