note 79960 added to language.oop5.overloading
| From: | egingell at sisna dot com | Date: | Fri, 21 Dec 2007 03:54:52 +0000 |
| Subject: | note 79960 added to language.oop5.overloading | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-133833@lists.php.net to get a copy of this message | ||
The PHP devs aren't going to implement true overloading because: PHP is not strictly typed by
any stretch of the imagination (0, "0", null, false, and "" are the same, for
example) and unlike Java and C++, you can pass as many values as you want to a function. The extras
are ignored unless you fetch them using func_get_arg(int) or func_get_args(), which is often how I
"overload" a function/method, and fewer than the declared number of arguments will
generate an E_WARNING, which can be suppressed by putting '@' before the function call,
but the function will still run as if you had passed null where a value was expected.
<?php
class someClass {
function whatever() {
$args = func_get_args();
// public boolean whatever(boolean arg1) in Java
if (is_bool($args[0])) {
// whatever(true);
return $args[0];
// public int whatever(int arg1, boolean arg2) in Java
} elseif(is_int($args[0]) && is_bool($args[1])) {
// whatever(1, false)
return $args[0];
} else {
// public void whatever() in Java
echo 'Usage: whatever([int], boolean)';
}
}
}
?>
// The Java version:
public class someClass {
public boolean whatever(boolean arg1) {
return arg1;
}
public int whatever(int arg1, boolean arg2) {
return arg1;
}
public void whatever() {
System.out.println("Usage: whatever([int], boolean)");
}
}
----
Server IP: 69.147.83.197
Probable Submitter: 65.100.198.135
----
Manual Page -- http://www.php.net/manual/en/language.oop5.overloading.php
Edit -- https://master.php.net/note/edit/79960
Del: integrated -- https://master.php.net/note/delete/79960/integrated
Del: useless -- https://master.php.net/note/delete/79960/useless
Del: bad code -- https://master.php.net/note/delete/79960/bad+code
Del: spam -- https://master.php.net/note/delete/79960/spam
Del: non-english -- https://master.php.net/note/delete/79960/non-english
Del: in docs -- https://master.php.net/note/delete/79960/in+docs
Del: other reasons-- https://master.php.net/note/delete/79960
Reject -- https://master.php.net/note/reject/79960
Search -- https://master.php.net/manage/user-notes.php