php类函数包装器


php class function wrapper

这是我的类:

class toyota extends car {
    function drive() {
    }
    function break() {
    }
}
class car {
    function pre() {
    }
}

有没有什么方法可以让我在运行$car->drive()、$car->break(或丰田中的任何其他函数)时,在调用丰田中的函数之前,先调用$car->pre()?

是的。你可以使用protected和一些__call魔法:

class toyota extends car {
    protected function drive() {
        echo "drive'n";
    }
    protected function dobreak() {
        echo "break'n";
    }
}
class car {
    public function __call($name, $args)
    {
        if (method_exists($this, $name)) {
            $this->pre();
            return call_user_func_array(array($this, $name), $args);
        }

    }
    function pre() {
        echo "pre'n";
    }
}
$car = new toyota();
$car->drive();
$car->dobreak();

http://ideone.com/SGi1g

您可以执行以下操作,但我认为这不是您想要的。

class toyota extends car {
    function drive() {
         $this->pre();
    }
    function break() {
         $this->pre();
    }
}
class car {
    function pre() {
    }
}

您可能想要研究PHP特定的魔术方法。http://php.net/manual/en/language.oop5.magic.php

这将更好地使用名为__call()的魔术方法

public function __call($name, $arguments)
{
    $this -> pre();
    return $this -> $name($arguments);
}

这种方法是什么它覆盖默认的方法调用,以便可以调用preCall State。

您的toyota

class toyota extends car {    
    public function __call($name, $arguments)
    {
        $this -> pre();
        return call_user_func_array(array($this, $name), $arguments);
    }
    function drive() {
    }
    function break() {
    }
}

如果您使用PHP5(>=5.3.2),有一个解决方案可以将所有方法声明为私有方法。这将强制执行来自单个函数调用的方法调用:

exec_method()

要在以下位置运行:http://ideone.com/cvfCXm

代码片段在这里:

<?php
    class car {
        //method to get class method
        public function get_method($method_name) {
            $class = new ReflectionClass(get_class($this));
            $method = $class->getMethod($method_name);
            $method->setAccessible(true);
            return $method;
        }
        public function exec_method($method_name, $arg_args=array()) {
            //execute the pre() function before the specified method
            $this->pre();
            //execute the specified method
            $this->get_method($method_name)->invokeArgs($this, $arg_args);
        }
        public function pre() {
            echo 'pre';
            echo '<br />';
        }
    }
    class toyota extends car {
        private function drive() {
            echo 'drive';
            echo '<br />';
        }
        private function brake() {
            echo 'brake';
            echo '<br />';
        }
    }
    $toyota = new toyota();
    $toyota->exec_method('drive');
    $toyota->exec_method('brake');
?>

参考:

使用PHPUnit〔closed〕测试受保护方法的最佳实践答案

只需添加一个构造函数,如下所示。。。

class toyota extends car {
    function __construct() { 
        $this->pre();
    }   
    function drive() {
        echo "drive!";
    }
    function dobreak() {
        echo "break!";
    }
}
class car {
    function pre() {
        echo "Hello!";
    }
}
$car = new toyota();
$car->drive();
$car->dobreak();

具有构造函数方法的类在每个新创建的对象,因此它适用于任何初始化该对象在使用之前可能需要。

break是保留的,所以不应该将其用作函数名。