Req #69961 [NEW]: Method overloading with PHP 7
| From: | simobj at live dot com | Date: | Mon, 29 Jun 2015 10:16:44 +0000 |
| Subject: | Req #69961 [NEW]: Method overloading with PHP 7 | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-193985@lists.php.net to get a copy of this message | ||
From: simobj at live dot com
Operating system: Linux
PHP version: 7.0.0alpha2
Package: SPL related
Bug Type: Feature/Change Request
Bug description:Method overloading with PHP 7
Description:
------------
Hello,
Maybe this would finally reach someone's heart; as method overloading is
really important to Object Oriented, especially now that PHP 7 will
support type hinting for primitives.
Imagine a StringBuilder class (which I'm really creating), now a
StringBuilder builds a string by appending/prepending a string, any
primitive type, another StringBuilder but not an object.
class StringBuilder {
public function append(StringBuilder $stringBuilder) {
$this->string += $stringBuilder->toString();
}
public function append(string $string) {
$this->string += $string;
}
}
This simply can't be achieved right now, and there's really no reason
since would be allowed to hint in PHP7.
One "get-around" solution right now is to use __call method to dispatch
to distinct methods, this way it appears as if we are using one single
method, but really we are using distinct methods. The problem is that 2
methods might have different arguments, so we have to use
func_num_args() to distinguish which method to use, followed by a
call_user_func_array :
class Foo {
private function doSomethingWith2Parameters($a, $b) {
}
private function doSomethingWith3Parameters($a, $b, $c) {
}
public function __call($method, $arguments) {
if($method == 'doSomething') {
if(count($arguments) == 2) {
return
call_user_func_array(array($this,'doSomethingWith2Parameters'),
$arguments);
}
else if(count($arguments) == 3) {
return
call_user_func_array(array($this,'doSomethingWith3Parameters'),
$arguments);
}
}
}
}
--
Edit bug report at https://bugs.php.net/bug.php?id=69961&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=69961&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=69961&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=69961&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=69961&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=69961&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=69961&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=69961&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=69961&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=69961&r=support
Expected behavior: https://bugs.php.net/fix.php?id=69961&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=69961&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=69961&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=69961&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=69961&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=69961&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=69961&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=69961&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=69961&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=69961&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=69961&r=mysqlcfg