note 51188 added to language.oop5
| From: | hans at lintoo dot dk | Date: | Wed, 23 Mar 2005 05:01:11 +0000 |
| Subject: | note 51188 added to language.oop5 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-86823@lists.php.net to get a copy of this message | ||
Multiple methods with same name but different signature does not compile.
trying to do:
<?php
public function __construct(string $myString) {
// do something with string
}
public function __construct(int $myInt) {
// do something with int
}
?>
does not work, perhaps because you cant do type hinting with string and int, but then you should be
able to create them as objects, which would give:
<?php
public function __construct(String $myString) {
// do something with string
}
public function __construct(Integer $myInt) {
// do something with int
}
?>
but that don't work either so you need to do:
<?php
public function __construct($myString=null,$myInt=null) {
if (!is_null($myInt)) {
// do something with int
} else if (!is_null($myString)) {
// do something with string
} else {
throw new Exception("Error: __construct called without string *OR* int";
}
}
?>
Hans Duedal // http://www.lintoo.dk/
----
Manual Page -- http://www.php.net/manual/en/language.oop5.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+51188
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51188&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51188&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51188&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51188&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51188&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+51188&report=yes
Search -- http://master.php.net/manage/user-notes.php