PHP 致命错误:声明 ..必须兼容


PHP Fatal error: Declaration of ... must be compatible with

我找不到问题。此代码可以工作多年,但是在重置服务器后,显示了此致命错误。也许你可以帮我。谢谢

PHP 致命错误:声明 styBoxLoadEventArgs::__construct() 必须与 EventArgs::__construct() 兼容 .../modules/sty/events/styBoxLoadEventArgs.php 在第 4 行

代码为:

<?php
class styBoxLoadEventArgs extends EventArgs
{
    public $hide = false;
    public $box;
    public function __construct($box)
    {
        $this->box = $box;
    }
}
?>

这是事件参数的代码

?php
/**
 * EventArgs
 */
abstract class EventArgs
{
    public $sender, $senderClass, $senderObject, $event;
    protected $items;
    /**
     * Creates new EventArgs object
     */
    abstract public function __construct();
    /**
     * Returns specified item
     * 
     * @param   string      item-name
     * @return  mixed
     */
    public function __get($_name)
    {
        if(!array_key_exists($_name, $this->items)) 
        {
            throw new BasicException("Item '" . $_name . "' not found");
        }
        return $this->items[$_name];
    }
    /**
     * Sets specified item
     * 
     * @param   string      item-name 
     * @param   mixed       value
     */
    public function __set($_name, $_value)
    {
        if(!array_key_exists($_name, $this->items))
        {
            throw new BasicException("Item '" . $_name . "' not found");
        }
        $this->items[$_name] = $_value;
    }
    public function &GetByRef($_key)
    {
        if(!array_key_exists($_key, $this->items))
        {
            throw new BasicException("Item '" . $_key . "' not found");
        }
        return $this->items[$_key];
    }
}

当子项重新声明父级定义的函数时,您必须为该函数保留相同的提示/数据类型。因此,假设EventArgs在其构造函数中请求特定的数据对象(或者在 PHP7 中使用严格的类型提示)。您的孩子还必须指定该数据类型。这是一个例子(编造的)

class EventArgs
{
    public function __construct(Array $box)
    {
        $this->box = $box;
    }
}

在这种情况下,您的子构造函数还必须要求Array。这有一些详细的原因