note 29784 deleted from ref.overload by sobak
| From: | sobak@php.net | Date: | Tue, 22 Nov 2016 03:48:33 +0000 |
| Subject: | note 29784 deleted from ref.overload by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207440@lists.php.net to get a copy of this message | ||
Note Submitter: steve at walkereffects dot com
----
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]
}
}
}