note 88403 added to function.overload

From: Date: Thu, 22 Jan 2009 15:22:43 +0000
Subject: note 88403 added to function.overload
Groups: php.notes 
Request: Send a blank email to php-notes+get-149662@lists.php.net to get a copy of this message
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. ---- Server IP: 219.239.98.206 Probable Submitter: 41.221.17.249 ---- Manual Page -- http://www.php.net/manual/en/function.overload.php Edit -- https://master.php.net/note/edit/88403 Del: integrated -- https://master.php.net/note/delete/88403/integrated Del: useless -- https://master.php.net/note/delete/88403/useless Del: bad code -- https://master.php.net/note/delete/88403/bad+code Del: spam -- https://master.php.net/note/delete/88403/spam Del: non-english -- https://master.php.net/note/delete/88403/non-english Del: in docs -- https://master.php.net/note/delete/88403/in+docs Del: other reasons-- https://master.php.net/note/delete/88403 Reject -- https://master.php.net/note/reject/88403 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#149662) next »