When you are using the same function in a parent class and a child class, but the child class needs parameters while the parent one not, you'll get the Strict Standards
error.
Example
Manager:
public function getAtPosition($position)
{
foreach ($this->getList() as $obj)
{
if ($obj->getPosition() == $position)
return $obj;
}
return null;
}
MenuManager extends Manager:
public function getAtPosition($position, $parent)
{
foreach ($this->getList() as $m)
{
if ($m->getParent() == $parent && $m->getPosition() == $position)
return $m;
}
return null;
}
This example will generate an error:
Strict standards: Declaration of MenuManager::getAtPosition() should
be compatible with Manager::getAtPosition($position)
Because we don't have the same arguments to the function, so let's trick this and add arguments, even though we're not using them!
Manager:
public function getAtPosition($position, $dummy = 0) // Dummy to avoid Strict standards errors
{
foreach ($this->getList() as $obj)
{
if ($obj->getPosition() == $position)
return $obj;
}
return null;
}
MenuManager extends Manager:
public function getAtPosition($position, $parent = 0)
{
foreach ($this->getList() as $m)
{
if ($m->getParent() == $parent && $m->getPosition() == $position)
return $m;
}
return null;
}
Only one to be careful is that when using getAtPosition()
from MenuManager.class.php
, be sure you are actually sending 2 parameters, as we have to declare $parent = 0
in order to match the parent's declaration.
Every class extending Manager
and not containing getAtPosition()
will use the method from Manager
.
If declared in a child class, php will use the method from the child class instead of the parent's one. There is no overloading
in PHP, so that is how I worked around it until it is properly implemented.