扩展类,函数定义类型提示&严格的警告

时间:2015-12-16 12:34:54

标签: php zend-framework

我正在尝试在相对较大的Zend代码库上进行维护工作,部分工作包括从php 5.3到php 5.6的工作。

设置error_reporting = E_ALL后,我看到了这些通知 几乎网站的每一页:

PHP Strict Standards:  Declaration of <child::method()> should be compatible 
with <parent::method()>

我创建了一个人为的例子来证明这个问题:

cat > doStuff.php <<"EOF"
<?php
error_reporting(E_ALL);
require_once('Potato.php');
require_once('PotatoMapper.php');
$potato = new App_Model_Potato();
$potatoMapper = new App_Model_PotatoMapper();
$potatoMapper->eat($potato);
?>
EOF

cat > Model.php <<"EOF"
<?php
abstract class App_Model {
    protected $vegetable = False;
}
?>
EOF

cat > Mapper.php <<"EOF"
<?php
abstract class App_Mapper{
    public function eat(App_Model $cls) { 
        echo "App_Mapper->eat()\n";
    }
}
?>
EOF

cat > Potato.php <<"EOF"
<?php
require_once('Model.php');
class App_Model_Potato extends App_Model {
    protected $potato = True;
}
?>
EOF

cat > PotatoMapper.php <<"EOF"
<?php
require_once('Mapper.php');
class App_Model_PotatoMapper extends App_Mapper{
    public function eat(App_Model_Potato $cls){ 
        echo "App_Model_PotatoMapper->eat()\n";
    }
}
?>
EOF

运行示例时,生成的通知是:

# php doStuff.php
PHP Strict Standards:  Declaration of App_Model_PotatoMapper::eat() should be
compatible with App_Mapper::eat(App_Model $cls) in /tmp/bs2/PotatoMapper.php
on line 3

PotatoMapper的eat()函数是:

public function eat(App_Model_Potato $cls)

PotatoMapper扩展了基础Mapper类,其eat()就是这样:

public function eat(App_Model $cls) { echo "App_Mapper->eat()\n";}

示例中是否存在任何内在错误?

如何重构此示例以修复通知?

0 个答案:

没有答案