首页 > 解决方案 > PHP 5.3 Zend - 看不到方法是如何被调用的

问题描述

我遇到了一个我以前没有遇到过的问题。在一个项目的类中(我相信它是基于Zend框架的),我有一个名为 的方法fetchKey(),完整的类名是

//found in file Orm.class.php
MyCompany_Core_Orm extends MyCompany_Core_Db (which extends MyCompany_Core)

在 fetchKey 内部,有以下行:

$value = $this->$property;

通过使用 ORM SQL 日志记录(logQueries=1),我可以缩小这一行导致 SQL 查询发生的范围;有了它,查询就会发生,没有它就不会。

$property是一个字符串,我已经检查过了,该字符串名称没有实际属性。实际上,$property可以是基于 db 关系的任意数量的值。但关键问题是:老实说,当一个方法仅被引用时,一个未知的函数调用是如何发生的,我真的很困惑。是否存在错误报告或使用__call__get我遗漏的方面?感谢帮助!

作为参考,下面发布了该功能,如果这有助于熟悉:

public function fetchKey($property)
{
    //NOTE: I've checked and this logical block doesn't happen
    if (self::getOrmProxy() && method_exists(self::getOrmProxy(), __FUNCTION__)) {
        $proxyArguments = func_get_args();
        return call_user_func_array(array(self::getOrmProxy(), __FUNCTION__), $proxyArguments);
    }

    $savedPropertyValue = null;
    $propertyExists = false;
    if (property_exists($this, $property)) {
        $propertyExists = true;
        $savedPropertyValue = $this->$property;
        unset($this->$property);
    }

    $value = $this->$property;

    if ($propertyExists === true) {
        $this->$property = $savedPropertyValue;
    } else {
        unset($this->$property);
    }

    return $value;
}

标签: phpmethodsproperties

解决方案


该函数property_exist()无法检测使用get()方法可以神奇访问的属性但是该函数甚至可以找到私有属性。


推荐阅读