Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /share/CACHEDEV1_DATA/Web/www/libraries/UBBcode/text_parser.class.php on line 228
Overloading in PHP - fact or fiction?

Comments Blog About Development Research Sites

Overloading in PHP - fact or fiction?

Dec 16, 2007
Those guys over at zend like to give you the impression that overloading is definately something PHP can do. Unfortunately, they're a bunch of weirdo's who tend to say stuff like that without really meaning it.

They wrote an entire manual page titled overloading, but as pointed out repeatedly in the comments, 'real' overloading is still somewhat of a no-go. Now, in my opinion this doesn't have to be the case. Although I wouldn't call it pretty I daresay it is still definately possible to call a different member based on what type of argument is given:
Code (php) (nieuw venster):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

class A {
  public function
__call ($member, $arguments) {
    if(
is_object($arguments[0]))
      $member = $member . 'Object';
    if(
is_array($arguments[0]))
      $member = $member . 'Array';
    $this -> $member($arguments);
  }

  private function
testArray () {
    echo
"Array.";
  }
  
  private function
testObject () {
    echo
"Object.";
  }
}

class
B {
}


$class = new A;
$class -> test(array()); // Echo 'Array.'
$class -> test(new B);   // Echo 'Object.'
?>


Of course this would be so much easier if PHP allowed the creation of two memberfunctions with the same name but different arguments. I have good hopes they might in some new version, but for now I am forced to agree that overloading, although possible, is too cumbersome to be really usefull.

FragFrog out!

New comment

Your name:
Comment: