note 79960 deleted from language.oop5.overloading by cmb

From: Date: Sun, 18 Aug 2019 08:26:22 +0000
Subject: note 79960 deleted from language.oop5.overloading by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-211902@lists.php.net to get a copy of this message
Note Submitter: egingell at sisna dot com ---- 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)"); } }

« previous php.notes (#211902) next »