【问题标题】:parent::method, method is being forced to lowercase causing method to not be foundparent::method,方法被强制小写导致找不到方法
【发布时间】:2023-04-05 19:03:01
【问题描述】:

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

我有一个类扩展了另一个类。当我尝试使用 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。

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

更新

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

【问题讨论】:

  • 你能发布两个类的实际代码吗?我以前从未见过这种行为,如果没有实际将parent::getid() 写成小写字母,我也无法在 PHP 5.3 上验证它。
  • @Webnet:你从哪里得到的代码?你怎么知道是OP的代码?
  • 因为他是我的同事^_^
  • 顺便说一句,Murilo Vasconcelos 在他现在已删除的答案中所说的是正确的:你没有调用正确的吸气剂。

标签: php class methods parent lowercase


【解决方案1】:

我尝试验证链接到 PHP 5.3.3 编辑中的 that comment 中的代码,但我得到了

A getTest
B getTest

相对于评论的输出

A getTest
B gettest

所以我唯一能想到的是您正在使用其他版本的 PHP,并且您遇到的这种行为是一个错误(无论是否退化)。

编辑:找到了,indeed a bug 已在 PHP 5.2.10 中修复:

如果在子类中调用parent::&lt;method-name&gt;(注意:这*不是*静态调用),并且父类中不存在&lt;method-name&gt;,则为父类的__call()魔术方法提供方法名称( $name 参数)小写。

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

【讨论】:

  • PHP 版本:实时:5.2.6 本地:5.3.2(实时是我遇到问题的地方,本地它工作得很好)。
  • @Chris Flynn:看我的编辑,显然这个错误出现在 5.2.6 中。
  • @Webnet,@Chris:没问题 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-11
  • 1970-01-01
  • 1970-01-01
  • 2015-11-24
  • 1970-01-01
  • 2014-11-09
相关资源
最近更新 更多