php strict error by including -
it looks strict errors not occur if classes in 1 file this:
abstract class food{} class meat extends food{} abstract class animal{ function feed(food $food){ } } class cat extends animal{ function feed(meat $meat){ parent::feed($meat); } } but if put class definition in separate files , include them that:
abstract class food{} class meat extends food{} require 'animal.php'; require 'cat.php'; strict standards error message thrown:
strict standards: declaration of
cat::feed()should compatibleanimal::feed(food $food)in c:\path\to\cat.php on line...
if in 1 file ok:
class dog extends animal{ function feed($qty = 1){ for($i = 0; $i < $qty; $i++){ $meat = new meat(); parent::feed($meat); } } } is intended behavior?
because meat food, there shouldn't complain in first place, right? solution plain , clear: put in 1 file , strict standards satisfied ;)
any hints appreciated
is intended behavior?
unfortunately, yes. intricacies come play class declarations make strict rules aren't applied when occur in same script; single class per file doesn't exhibit issue.
because meat food, there shouldn't complain in first place, right?
wrong 2 reasons:
meat smaller type food , therefore allowing smaller type in descendent class you're violating lsp; can't substitute
catanimal.in php, argument types invariant when overloading methods, i.e. accepted types must match of parent. while argued contravariant types make sense, due technical reasons can't done.
so solution plain , clear: put in 1 file , strict standards satisfied ;)
no, , should not rely on behaviour.
Comments
Post a Comment