note 91327 added to language.oop5.overloading
| From: | jesdisciple at gmail dot com | Date: | Fri, 05 Jun 2009 23:32:20 +0000 |
| Subject: | note 91327 added to language.oop5.overloading | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-156409@lists.php.net to get a copy of this message | ||
I submitted this message earlier but forgot to fill out my email address. I hope I've waited
long enough for it to show up, but if not I apologize.
maciej at killer-robot dot net said:
> Just a note in the interest of sparing people some confusion if they are coming
> from or reading about other object-oriented languages: Object "overloading"
> in PHP has nothing to do with overloading as it is commonly understood.
> Normally, "overloading" refers to the ability to declare multiple functions with
> the same name, which get called based on the number/type of parameters --
> this does not exist in PHP. In PHP, "overloading" is just a mechanism for
> providing handlers for access to missing methods/variables.
Actually, it is possible to accomplish method polymorphism via PHP's __call method:
<?php
class Example{
public function __call($name, $arguments){
switch($name){
case 'foo':
switch(count($arguments)){
case 2:
echo 'You called "foo" with two arguments.<br>' .
PHP_EOL;
break;
case 3:
echo 'You called "foo" with three arguments.<br>'
. PHP_EOL;
break;
default:
echo 'Error: Invalid number of arguments to
"foo."<br>' . PHP_EOL;
break;
}
break;
default:
echo "Error: Call to undefined function \"$name.\"<br>" .
PHP_EOL;
}
}
}
$test = new Example;
$test->foo('bar', 'baz');
$test->foo('bar', 'baz', 'fez', 'fap');
$test->bar();
?>
----
Server IP: 64.71.164.2
Probable Submitter: 76.222.26.13
----
Manual Page -- http://www.php.net/manual/en/language.oop5.overloading.php
Edit -- https://master.php.net/note/edit/91327
Del: integrated -- https://master.php.net/note/delete/91327/integrated
Del: useless -- https://master.php.net/note/delete/91327/useless
Del: bad code -- https://master.php.net/note/delete/91327/bad+code
Del: spam -- https://master.php.net/note/delete/91327/spam
Del: non-english -- https://master.php.net/note/delete/91327/non-english
Del: in docs -- https://master.php.net/note/delete/91327/in+docs
Del: other reasons-- https://master.php.net/note/delete/91327
Reject -- https://master.php.net/note/reject/91327
Search -- https://master.php.net/manage/user-notes.php