php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #40979 Method Overloading by Method Signature
Submitted: 2007-04-02 17:30 UTC Modified: 2007-04-21 12:26 UTC
From: wjaspers at nuaire dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 6CVS-2007-04-02 (CVS) OS: CentOS / Windows XP Pro
Private report: No CVE-ID: None
 [2007-04-02 17:30 UTC] wjaspers at nuaire dot com
Description:
------------
Please add the ability to overload methods by their signature. This feature would be the same as Java 5, in that a class could be created with several functions by the same name, but the PHP engine could determine which one to use simply by determining which method signature is being utilized.
I would also hope if this is created, that Constructors may also be overloaded by signature. (This would make porting java applications to PHP or vice-versa a cinch).

Reproduce code:
---------------
As an example, the class below has two constructors with the same name, and three methods with the same name, which would be differentiated simply by the type and number of variables supplied.

This would make PHP much more object-oriented, and more robust.

<?php
class TestMe
{
  var $isValid = false; # intial value
  public function TestMe() {
     $this->isValid = false;
  }
  public function TestMe(bool $isValid) {
     $this->isValid = $isValid;
  }
  public function validate() {
  }
  public function validate(bool $strict) {
  }
  public function validate(Validator $validatorObject) {
  }
}
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-04-20 18:44 UTC] wjaspers at nuaire dot com
I would also like to add:

Could future versions of PHP  PLEASE, PLEASE, PLEASE  recognize  basic data types such as 'char','int',and 'string'  as they are passed through the method signature. That way, if the wrong data type is submitted, a ClassCastException or other situation can occur to indicate either to the programmer or the end-user that the data supplied is not correct?

An example as such:

<?php
class Me {
[...]
function setMyName(String $myName)
{
 $this->name = $myName;
}
[...]
}
?>
 [2007-04-21 12:26 UTC] johannes@php.net
This was discussed and rejected multiple times. Please see the archives of the php-internals list.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 01 05:01:29 2024 UTC