note 88403 deleted from function.overload by sobak
| From: | sobak@php.net | Date: | Sun, 04 Dec 2016 07:38:41 +0000 |
| Subject: | note 88403 deleted from function.overload by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208137@lists.php.net to get a copy of this message | ||
Note Submitter: osminosm at gmail dot com
----
creating two methods with the same name won't work for sure (maybe in next versions i hope)
but for now all i could come up with something that looks like overloaded functions from the outside
but still makes it a bit difficult for the one who's coding the actual class
as we can see in the code bellow i've used a default value for the $name argument, so when the
Test() method is called with no arguments the $name argument is by default passed as NULL (or any
value you wanna pass)
<?php
class Test
{
function Test($name=NULL)
{
echo 'Hello, ';
if($name)
{
echo $name.'<br>';
}
else
{
echo 'stranger<br>';
}
}
}
$t1=new Test(); // Output : Hello, stranger
$t2=new Test('Osman Kalache'); // Output : Hello, Osman Kalache
?>
the bad side of this trick is that you have to test your arguments (imagine how many IFs and ELSEs
you get if you had just 5 arguments)
but still makes your classes easy to use.