在PHP 5.X中是否有可能在调用类方法时,在被调用函数之前执行的类中有一个方法?我需要这个,因为我想对被调用函数中使用的参数进行一些动态验证.
class MyClass {
protected function preHook(){ echo "You are about to call a method."; }
public function methodA() { echo "You called methodA."; }
}
$obj = new MyClass();
$obj->methodA();
// Output: "You called methodA."
// Desired output: "You are about to call a method.You called methodA"
Run Code Online (Sandbox Code Playgroud)
还要记住以下内容:"methodA"需要公开,因为在代码中使用反射来检测方法.
正如在注释中提到的那样,这是不可能的,因为只有在没有定义具有给定名称的方法时才调用__call魔术方法:
http://php.net/manual/en/language.oop5.magic.php
然而,也许以下丑陋,黑客的解决方案之一将满足您.
解决方案1
将需要更改所有方法名称:
class MyClass {
public function __call($name, $arguments){
echo "You are about to call $name method.";
return call_user_func_array(array($this, '_real_' . $name), $arguments);
}
private function _real_methodA() { echo "You called methodA."; }
}
$obj = new MyClass();
$obj->methodA();
Run Code Online (Sandbox Code Playgroud)
解决方案2
这将需要一个'包装'类:
class MyClass {
public function methodA() { echo "You called methodA."; }
}
class MyClassWrapper {
public function __construct(){
$this->myClass = new MyClass();
}
public function __call($name, $arguments){
echo "You are about to call $name method.";
return call_user_func_array(array($this->myClass, $name), $arguments);
}
}
$obj = new MyClassWrapper();
$obj->methodA();
Run Code Online (Sandbox Code Playgroud)
解决方案3
第三种方法是应用装饰器模式并创建一个包装类.
class Decorator
{
protected $_instance;
public function __construct($instance)
{
$this->_instance = $instance;
}
public function __call($method, $args)
{
print 'do your stuff here';
return call_user_func_array(array($this->_instance, $method), $args);
}
}
$obj = new Decorator(new MyClass);
$obj->methodA();
Run Code Online (Sandbox Code Playgroud)
解决方案4
混合解决方案1并使用反射和"runkit_method_rename"重命名所有方法 http://docs.php.net/manual/en/function.runkit-method-rename.php runkit是实验性的,所以这是相当硬的.
class MyClass {
public function __call($name, $arguments){
echo "You are about to call $name method.";
return call_user_func_array(array($this, '_real_' . $name), $arguments);
}
private function methodA() { echo "You called methodA."; }
}
$reflection = new ReflectionClass('MyClass');
$methods = $reflection->getMethods();
foreach ($methods as $method) {
runkit_method_rename('MyClass', $method->name , '_real_' . $method->name);
}
$obj = new MyClass();
$obj->methodA();
Run Code Online (Sandbox Code Playgroud)