Parent::method,方法被强制小写,导致找不到方法
这个问题很简单,我确定,我只是不知道答案。
我有一个类扩展了另一个类。当我尝试 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尝试验证该评论中链接到的代码在 PHP 5.3.3 中编辑,但我得到
与注释的输出相反,
所以我唯一能想到的是,您正在使用 PHP 的其他版本,并且您遇到了该行为作为错误(是否回归) 。
编辑:找到它,确实是一个错误,已在PHP 5.2.10:
I tried to verify the code in that comment linked to in the edit in PHP 5.3.3 but I get
As opposed to the comment's output of
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: