note 79110 deleted from language.oop5.interfaces by cmb

From: Date: Sun, 26 Jan 2020 09:39:34 +0000
Subject: note 79110 deleted from language.oop5.interfaces by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-213958@lists.php.net to get a copy of this message
Note Submitter: Docey ---- Another note about default values in interfaces is that an class must implement at least the arguments as in the interface. that is: an implementation may have more arguments but not less if these additional arguments have an default value and thus can be called as declared in the interface. an litte example: <?php interface myInterface{ public function setStuff($id, $name); } class MyFirstClass implements myInterface{ public function setStuff($id, $name); } class MySecondClass implements myInterface{ public function setStuff($id, $name, $type); } class myThirdClass implements myInterface{ public function setStuff($id, $name, $type=0); } ?> Here mySecondClass will print an fatal error while myThirdClass is just fine because myThirdClass::setStuff($id, $name); is valid and thus fullfills the interface requirements. an interface declares as set of requirement on how methods can be called and any class implementing an interface thus agrees that is will provide these methods and that they can be called as in the interface. adding additional arguments with default values is thus allowed because it does not violate the agreement that the method can be called as in the interface.

« previous php.notes (#213958) next »