PHP警告继承的函数声明不兼容
我的debug.log中有一个关于PHP严格标准的奇怪警告PHP警告继承的函数声明不兼容,php,oop,Php,Oop,我的debug.log中有一个关于PHP严格标准的奇怪警告 PHP Strict standards: Declaration of gb_EntryList::fields() should be compatible with mgr_LinearModelCollection::fields($mKey = NULL) in ... on line 339 到目前为止还很糟糕,但第339行保留了gb_EntryList类的定义 而gb_EntryList根本不定义字段。它从mgr_my
PHP Strict standards: Declaration of gb_EntryList::fields() should be compatible with mgr_LinearModelCollection::fields($mKey = NULL) in ... on line 339
到目前为止还很糟糕,但第339行保留了gb_EntryList类的定义
而gb_EntryList根本不定义字段。它从mgr_mySQLModel继承了以下内容:
最初,我忘记在声明中输入=null,这产生了关于mgr_mySQLModel的类似消息。但现在这些都不见了
代码运行得很好,但是这条消息想告诉我什么
PHP版本:
PHP 5.4.4-14+deb7u5 (cli) (built: Oct 3 2013 09:24:58)
更新:
我对这个问题进行了更深入的研究。有趣的是,下面的代码应该具有相同的结构,但是对于php-d error_reporting=4095-l来说可以:
实际的类太大,无法在这里复制它们。但这种结构与接口继承是一样的。但是,上面的代码似乎没有问题。实际情况稍微复杂一些:
abstract class mgr_LinearModelCollection implements IteratorAggregate, Countable, ArrayAccess {
protected $sItemClass;
function getIterator() { }
function count(){ }
function offsetExists($offset){ }
function offsetGet($offset){ }
function offsetUnset($offset){ }
function offsetSet($offset, $value){ }
function fields($mKey = null){
$aDummy = call_user_func(array($this->sItemClass,'dummyArray'),$mKey);
return array_keys($aDummy);
}
}
interface mgr_mySQLModelUpdateable {
static function updateTable(array $aOpts = array());
}
abstract class mgr_mySQLModel extends mgr_LinearModelCollection implements mgr_mySQLModelUpdateable{
protected $aFieldNames = null;
function fields($mKey = null){
if(!is_array($this->aFieldNames)) return false;
return $this->aFieldNames;
}
}
class gb_EntryList extends mgr_mySQLModel {
static function updateTable(array $aOpts = array()){ }
}
当然还有更多的函数,{}中充满了代码,但除此之外,这是真正的未更改代码,它会产生上述错误消息。我现在不知道为什么玩具模型是好的,但真正的不是。有什么提示吗?您说该字段没有在gb\u EntryList中声明,这一事实就是错误的线索。mgr_mySQLModel是一个抽象类,如果您查看下面的提示,即当从抽象类继承时,父类声明中标记为ABSTRACT的所有方法都必须由子类定义。因此,尽管PHP神奇地解决了您想要父字段方法的问题,但您确实也应该在子类中声明它。此严格消息警告您违反了。这意味着已经在超类中定义的方法需要可以使用超类中方法的参数调用 这将触发以下消息:
class Super {
public function foo($argument) {
echo $argument;
}
}
class Child extends Super {
public function foo() {
parent::foo('bar');
}
}
输出:
Strict Standards: Declaration of Child::foo() should be compatible
with Super::foo($argument) in ... on line 15
请显示整个gb_EntryList类,如果太长,请剪切不相关的方法。gb_EntryList的任何其他父级是否声明方法字段?是的,mgr_mySQLModel::fields重载mgr_LinearModelCollection::fields。现在已经没有家长了,但是顶级类实现了IteratorAggregate、Countable和ArrayAccess。他正在写一个类似的答案;我发现这是一个常见的问题,人们总是忘记他们是在扩展一个抽象类,而不仅仅是一个普通类。如果这是一个问题,错误消息会非常明确地说:字段在层次结构中的任何地方都不是抽象的。我想应该是这样的。mgr_LinearModelCollection是第一个定义该函数的。mgr_mySQLModel重载它,但使用相同的参数模板。gb_EntryList只是继承,没有自己的定义。
abstract class mgr_LinearModelCollection implements IteratorAggregate, Countable, ArrayAccess {
protected $sItemClass;
function getIterator() { }
function count(){ }
function offsetExists($offset){ }
function offsetGet($offset){ }
function offsetUnset($offset){ }
function offsetSet($offset, $value){ }
function fields($mKey = null){
$aDummy = call_user_func(array($this->sItemClass,'dummyArray'),$mKey);
return array_keys($aDummy);
}
}
interface mgr_mySQLModelUpdateable {
static function updateTable(array $aOpts = array());
}
abstract class mgr_mySQLModel extends mgr_LinearModelCollection implements mgr_mySQLModelUpdateable{
protected $aFieldNames = null;
function fields($mKey = null){
if(!is_array($this->aFieldNames)) return false;
return $this->aFieldNames;
}
}
class gb_EntryList extends mgr_mySQLModel {
static function updateTable(array $aOpts = array()){ }
}
class Super {
public function foo($argument) {
echo $argument;
}
}
class Child extends Super {
public function foo() {
parent::foo('bar');
}
}
Strict Standards: Declaration of Child::foo() should be compatible
with Super::foo($argument) in ... on line 15