0

I can´t find the problem. This code works over years, but after reset the server this fatal-error was displayed. Maybe you can help me. Thank you

PHP Fatal error: Declaration of styBoxLoadEventArgs::__construct() must be compatible with EventArgs::__construct() in .../modules/sty/events/styBoxLoadEventArgs.php on line 4

the code is:

<?php

class styBoxLoadEventArgs extends EventArgs
{
    public $hide = false;
    public $box;

    public function __construct($box)
    {
        $this->box = $box;
    }
}
?>

here is the code of the EventArgs

?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];
    }

}

1 Answers1

1

When a child redeclares a function that a parent has defined, you have to keep the same hinting/data types for the function. So let's say EventArgs asks for a specific data object in its constructor (or in PHP7 uses strict type hints). Your child must also specify that data type. Here's an example (made one up)

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

In this case, your child constructor must also ask for an Array. There's some elaborate reasons for this

Community
  • 1
  • 1
Machavity
  • 30,841
  • 27
  • 92
  • 100