Parent::method,方法被强制小写,导致找不到方法

发布于 2024-10-16 10:36:10 字数 2284 浏览 2 评论 0原文

这个问题很简单,我确定,我只是不知道答案。

我有一个类扩展了另一个类。当我尝试 Parent::method 使用父类的功能时,我收到“调用未定义的方法 'parentClass'::getid()”。

它的作用是强制方法名称小写。从上面的例子来看,parent::getId() 被强制为parent::getid();

不知这是为什么?有什么想法吗?

代码示例

Class myClass extends OtherClass {  
   public function getProductList() {  
    //does other stuff  
     return parent::getId();
   }  
}

尝试运行parent::getid() 而不是parent::getId()。 getId() 只是父类(数据库模型类)的 getter。

也在本地工作,只是在我的测试版推送之后才发生这种情况。

update

parent::getId() 调用 __call 方法

/**
 * @method __call
 * @public
 * @brief Facilitates the magic getters and setters.
 * @description
 * Allows for the use of getters and setters for accessing data. An exception will be thrown for
 * any call that is not either already defined or is not a getter or setter for a member of the
 * internal data array.
 * @example
 * class MyCodeModelUser extends TruDatabaseModel {
 *  ...
 *  protected $data = array(
 *      'id' => null,
 *      'name' => null
 *  );
 *  ...
 * }
 * 
 * ...
 * 
 * $user->getId(); //gets the id
 * $user->setId(2); //sets the id
 * $user->setDateOfBirth('1/1/1980'); //throws an undefined method exception
 */
public function __call ($function, $arguments) {
    $original = $function;
    $function = strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $function));

    $prefix = substr($function, 0, 4);

    if ($prefix == 'get_' || $prefix == 'set_') {
        $key = substr($function, 4);

        if (array_key_exists($key, $this->data)) {
            if ($prefix == 'get_') {
                return $this->data[$key];
            } else {
                $this->data[$key] = $arguments[0];

                return;
            }
        }
    }

    $this->tru->error->throwException(array(
        'type' => 'database.model',
        'dependency' => array(
            'basic',
            'database'
        )
    ), 'Call to undefined method '.get_class($this).'::'.$original.'()');
}

下面是在 PHP.net 上抛出相同错误的示例: http://www.php.net/manual/en/keyword.parent.php#91315< /a>

This problem is pretty simple, im sure, i just dont know the answer.

I have a class that extends another class. When I try parent::method to use the functionality from the parent class, I get "Call to undefined method 'parentClass'::getid()".

What its doing is, it is forcing the method name to be lowercased. from the example above, parent::getId() is being forced to parent::getid();

I do not know why this is? Any thoughts?

Code example

Class myClass extends OtherClass {  
   public function getProductList() {  
    //does other stuff  
     return parent::getId();
   }  
}

tried to run parent::getid() instead of parent::getId(). getId() is just a getter on the parent class which is a database model class.

Also worked locally, its only after my beta push that this happend.

update

parent::getId() invokes the __call method

/**
 * @method __call
 * @public
 * @brief Facilitates the magic getters and setters.
 * @description
 * Allows for the use of getters and setters for accessing data. An exception will be thrown for
 * any call that is not either already defined or is not a getter or setter for a member of the
 * internal data array.
 * @example
 * class MyCodeModelUser extends TruDatabaseModel {
 *  ...
 *  protected $data = array(
 *      'id' => null,
 *      'name' => null
 *  );
 *  ...
 * }
 * 
 * ...
 * 
 * $user->getId(); //gets the id
 * $user->setId(2); //sets the id
 * $user->setDateOfBirth('1/1/1980'); //throws an undefined method exception
 */
public function __call ($function, $arguments) {
    $original = $function;
    $function = strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $function));

    $prefix = substr($function, 0, 4);

    if ($prefix == 'get_' || $prefix == 'set_') {
        $key = substr($function, 4);

        if (array_key_exists($key, $this->data)) {
            if ($prefix == 'get_') {
                return $this->data[$key];
            } else {
                $this->data[$key] = $arguments[0];

                return;
            }
        }
    }

    $this->tru->error->throwException(array(
        'type' => 'database.model',
        'dependency' => array(
            'basic',
            'database'
        )
    ), 'Call to undefined method '.get_class($this).'::'.$original.'()');
}

Here's an example that throws the same error on PHP.net: http://www.php.net/manual/en/keyword.parent.php#91315

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓬勃野心 2024-10-23 10:36:10

我尝试验证该评论中链接到的代码在 PHP 5.3.3 中编辑,但我得到

A getTest
B getTest

与注释的输出相反,

A getTest
B gettest

所以我唯一能想到的是,您正在使用 PHP 的其他版本,并且您遇到了该行为作为错误(是否回归) 。

编辑:找到它,确实是一个错误,已在PHP 5.2.10

如果在子类中调用 parent:: (注意:这*不是*静态调用),并且 < /code> 不存在于父级中,父级的 __call() 魔术方法提供小写的方法名称($name 参数)。


  • 修复了错误#47801(通过parent::运算符访问__call())提供了错误的方法名称)。 (费利佩)

I tried to verify the code in that comment linked to in the edit in PHP 5.3.3 but I get

A getTest
B getTest

As opposed to the comment's output of

A getTest
B gettest

So the only thing I can think of is that you're using some other version of PHP and you're encountering that behavior as a bug (regressed or not).

EDIT: found it, indeed a bug that was fixed in PHP 5.2.10:

If parent::<method-name> (NOTE: this is *not* a static invocation) is called in a child class, and <method-name> does not exist in the parent, the parent's __call() magic method is provided the method name (the $name argument) in lower case.

  • Fixed bug #47801 (__call() accessed via parent:: operator is provided incorrect method name). (Felipe)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文