note 79743 added to language.oop5.abstract
| From: | michikono at gmail dot com | Date: | Mon, 10 Dec 2007 23:29:50 +0000 |
| Subject: | note 79743 added to language.oop5.abstract | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-133342@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 64.71.164.2
Probable Submitter: 67.90.94.154
----
Manual Page -- http://www.php.net/manual/en/language.oop5.abstract.php
Edit -- https://master.php.net/note/edit/79743
Del: integrated -- https://master.php.net/note/delete/79743/integrated
Del: useless -- https://master.php.net/note/delete/79743/useless
Del: bad code -- https://master.php.net/note/delete/79743/bad+code
Del: spam -- https://master.php.net/note/delete/79743/spam
Del: non-english -- https://master.php.net/note/delete/79743/non-english
Del: in docs -- https://master.php.net/note/delete/79743/in+docs
Del: other reasons-- https://master.php.net/note/delete/79743
Reject -- https://master.php.net/note/reject/79743
Search -- https://master.php.net/manage/user-notes.php