note 29784 added to ref.overload
| From: | steve at walkereffects dot com | Date: | Tue, 25 Feb 2003 04:32:49 +0000 |
| Subject: | note 29784 added to ref.overload | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-44474@lists.php.net to get a copy of this message | ||
If you are a perfectionist when it comes to your class interfaces, and you are unable to use
overload(), there is another viable solution:
Use func_num_args() to determine how many arguments were sent to the function in order to create
virtual polymorphism. You can create different scenarios by making logical assumptions about the
parameters sent. From the outside the interface works just like an overloaded function.
The following shows an example of overloading a class constructor:
class Name
{
var $FirstName;
var $LastName;
function Name($first, $last)
{
$numargs = func_num_args();
if($numargs >= 2)
{
$this->FirstName = $first;
$this->LastName = $last;
}
else
{
$names = explode($first);
$this->FirstName = $names[0];
$this->LastName = $names[1]
}
}
}
--
http://www.php.net/manual/en/ref.overload.php
http://master.php.net/manage/user-notes.php?action=edit+29784
http://master.php.net/manage/user-notes.php?action=delete+29784
http://master.php.net/manage/user-notes.php?action=reject+29784