dongxiong2000 2011-02-08 17:22
浏览 76

parent ::方法,方法被强制为小写导致无法找到方法

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

  • 写回答

1条回答 默认 最新

  • dqsk4643 2011-02-08 17:42
    关注

    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)
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题