note 79743 deleted from language.oop5.abstract by felipe

From: Date: Sat, 15 Dec 2007 15:35:31 +0000
Subject: note 79743 deleted from language.oop5.abstract by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-133577@lists.php.net to get a copy of this message
Note Submitter: michikono at gmail dot com ---- When trying to enforce arguments on constructors, PHP behaves unexpectedly. First, look at this example where a constructor argument count and type are specified in an interface: <?php class ExampleClass {} interface TestInterface { public function __construct(ExampleClass $var); public function output(ExampleClass $var); } class Test implements TestInterface { // error, no constructor method public function output($var) {} // error, wrong argument type } ?> However, if you define the constructor, the __construct method argument datatype/count checks go out the window: <?php class Test implements TestInterface { public function __construct() {} // NO ERROR (unexpected) public function output(ExampleClass $var) {} // No error (expected) ?> Despite the data types and argument count being off, PHP doesn't care. Even if I define an argument in the constructor, the datatype check is ignored. So the best you can do is force a __construct() definition to be required, but you can't dictate its arguments (i.e., interfaces for constructor methods are useless). This problem produces the SAME results if instead of an interface, abstract methods in an abstract parent class are used.

« previous php.notes (#133577) next »