So I have this parent class:
class GenericHTMLElement
{
public function addElement(GenericHTMLElement $element) {}
}
that is extended by these two classes
class ListViewItem extends GenericHTMLElement
{
}
class ListView extends GenericHTMLElement
{
/**
* @param ListViewItem $element
* @throws WrongTypeException
*/
public function addElement(GenericHTMLElement $element)
{
if (!$element instanceof ListViewItem)
{
throw new WrongTypeException("ListViewItem", $element);
}
parent::addElement($element);
}
}
This works great. The problem is that it is not very clear that ListView::addElement()
only accepts a ListViewItem object as argument.
Moreover, when generating documentation, it assumes $element
should be of type GenericHTMLElement
even with that javadoc comment.
However, changing the typehint triggers the StrictStandards warning.
EDIT: changed the questions a bit...
Q. Why is changing parameters in method overriding a violation of Strict Standards? You can do that in other programming languages. And in PHP, you can do that in the constructor
Q. What are the implications of violating the Strict Standards in this case? As I understand it, the strict warning appears to warn the programmer that there's a possibility someone could read the signature of the parent class and assume it's children behaves the same way. That could lead to a runtime error and halt in execution. But that is precisely what I'm trying to achieve.
EDIT:
Since it was asked in the comments, more details about this...
This is part of a Template Engine from Mobile Web Applications that uses JQuery Mobile at its core.
JQuery mobile has specific requirements regarding how to structure a "page". jQueryMobile also uses specific attributes such as data-theme or data-position.
In order to manipulate the DOM of a page, you have to enforce that structure.
GenericHTMLElement represents a "generic" block of HTML (a div, a table, etc...). A ListView is the jQueryMobile equivalent of a ul or ol. A ListView can only have a ListViewItem (li) as children. A listView, for instance, has a property named data-filter that enables or disables a search filter.
more info at http://jquerymobile.com/test/docs/api/data-attributes.html