I want to be able to call a function automatically before calling any function. The problem with __callStatic is that it only executes if the method does not exist.
See code below.
I want always_run () to run before any function that is called in a 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 was called";
}
}
Test::my_func();
source
share