php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #69961 Method overloading with PHP 7
Submitted: 2015-06-29 10:16 UTC Modified: 2015-06-29 18:20 UTC
From: simobj at live dot com Assigned: kalle (profile)
Status: Closed Package: SPL related
PHP Version: 7.0.0alpha2 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: simobj at live dot com
New email:
PHP Version: OS:

 

 [2015-06-29 10:16 UTC] simobj at live dot com
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);
          }
      }
   }    
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-06-29 12:51 UTC] cmb@php.net
This is unlikely to be implemented, see request #40979 and
<http://marc.info/?l=php-internals&m=121397217528280&w=2>.
 [2015-06-29 18:20 UTC] kalle@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: kalle
 [2015-06-29 18:20 UTC] kalle@php.net
Like Christoph said; it is unlikely to be implemented. What you could do is to write an RFC and present it to the mailing lists (it usually helps even more if you can provide a patch).

See our wiki at: http://wiki.php.net/ more information on RFCs.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 15:01:34 2025 UTC