调用运行时创建的函数


invoking runtime created functions

我正在尝试为我正在处理的项目动态创建数据库实体泛化的基础。我基本上想为扩展它的任何类中的属性动态创建一组标准方法和工具。就像你用Python/Django免费获得的工具一样。

我从这个人那里得到了一个想法:http://www.stubbles.org/archives/65-Extending-objects-with-new-methods-at-runtime.html

所以我实现了上面帖子中所述的__call函数,

public function __call($method, $args) {
    echo "<br>Calling ".$method;
    if (isset($this->$method) === true) {
        $func = $this->$method;
        $func();
    }
}

有一个函数,它通过get_object_vars为我提供对象公共/受保护的属性,

public function getJsonData() {
    $var = get_object_vars($this);
    foreach($var as &$value) {
        if (is_object($value) && method_exists($value, 'getJsonData')) {
            $value = $value->getJsonData;
        }
    }
    return $var;
}

现在我想为他们创建一些方法:

public function __construct() {
    foreach($this->getJsonData() as $name => $value) {
        // Create standard getter
        $methodName = "get".$name;
        $me = $this;
        $this->$methodName = function() use ($me, $methodName, $name) { 
            echo "<br>".$methodName." is called";
            return $me->$name; 
        };
    }
}

感谢Louis H.在下面指出了"use"关键字。这基本上是动态创建一个匿名函数。该函数是可调用的,但它不再在其对象的上下文中。它产生"致命错误:无法访问受保护的财产"

不幸的是,我绑定了PHP版本5.3,它排除了Closure::bind。因此,PHP 中延迟加载类方法中建议的解决方案在这里不起作用。

我在这里相当难倒...还有其他建议吗?

更新

为简洁起见进行了编辑。

像这样尝试(你必须使你需要的变量可用于该方法)

$this->$methodName = function() use ($this, $methodName, $name){ 
    echo "<br>".$methodName." is called";
    return $this->$$name; 
};

您应该能够通过 $this 访问对象上下文。

我没有更新上面的原始问题,而是在这里为任何遇到相同问题的人提供完整的解决方案:

首先,由于闭包

不能有真正的对象访问权限,因此在创建闭包函数时,我需要在"use"声明中包含实际值(请参阅上面的原始__construct函数):

$value =& $this->$name;
$this->$methodName = function() use ($me, $methodName, &$value)  { 
    return $value; 
};

其次,__call魔术方法不仅需要调用闭包函数,还需要返回它的任何输出。因此,我不只是调用 $func(),而是返回 $func();

这成功了! :-)