php - adding parameters to overriden method E_STRICT observation -
php - adding parameters to overriden method E_STRICT observation -
it appears (php 5.3) if overriding class method, it okay to can add together additional parameters, long have default values.
for example, consider class:
class test1 { public function stuff() { echo "hi"; } } the next class extends "test1" , produce e_strict warning.
class test2 extends test1 { public function stuff($name) { echo "hi $name"; } } but, next not produce e_strict warning.
class test3 extends test1 { public function stuff($name = "") { echo "hi $name"; } } while class "test3" doesn't produce e_strict warning, have been under impression php not allow method signatures overloaded overridden. so, have ask. observation bug/flaw or correct intended behavior?
further, if default argument parameter okay, why non-default argument parameter not okay?
if add together non-defaulted parameter overridden method, subclass no longer satisfies contract defined superclass. cannot correctly phone call test2->stuff(), because method expects parameter - superclass says should able phone call without one. hence e_strict warning.
if add together defaulted parameter though, can still phone call test3->stuff() (from example) - superclass expects - , contract not broken. in fact, adding optional parameter, have extended it.
php oop coding-style
Comments
Post a Comment