frustrated Posted February 3, 2006 Share Posted February 3, 2006 Hi, I'm not sure if it's called method overloading or property overloading but here's what I want to do. In a language like Java I could do something like the following (I'll use php syntax but let's pretend it's java)[code]class test { private $id; public function __construct() { } public function __construct($id) { $this->id = $id; } }[/code]If this were done in Java, when a test object was instantiated, based on if you passed it a parameter or not it would know how to set its self up. If you passed a parameter it would call the second constructor, if not it would call the first. You could do this with any methods...name them the same thing but give them a different amount of parameters, and based on how many parameters you pass the method it will find the appropriate one with the same amout of parameters.I'm trying to figure out if I can do this with PHP, I've been searching but I can't really find anything that makes sense.Could someone unveil this mystery for me please?? Quote Link to comment https://forums.phpfreaks.com/topic/3315-php-5-method-overloading/ Share on other sites More sharing options...
daiwa Posted February 4, 2006 Share Posted February 4, 2006 I know thats one of the things i really miss from other languages. but yes you can implement it but its not that pretty. basically u need to use a factoryclass test{ private $id; public function __construct() { $args = func_num_args(); switch($args){case(1).... anyways you get the point call construct from ID functionbreakcase (2) and so on } }} Quote Link to comment https://forums.phpfreaks.com/topic/3315-php-5-method-overloading/#findComment-11306 Share on other sites More sharing options...
frustrated Posted February 4, 2006 Author Share Posted February 4, 2006 [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]yes you can implement it but its not that pretty[/quote]You're right about that, but I guess if that's the only way then it'll work. Thanks for the feedback. Quote Link to comment https://forums.phpfreaks.com/topic/3315-php-5-method-overloading/#findComment-11307 Share on other sites More sharing options...
gizmola Posted February 4, 2006 Share Posted February 4, 2006 Although Daiwa offers an interesting facility, I think it's pretty clear that using a switch() isn't a substitute for function overloading. In function overloading you could simply have a parameter with the same name, only a different datatype as a parameter, and that would be enough to differentiate an overloaded function. Since PHP really doesn't care about what type of parameter you pass to a function, it's just not a good platform for function overloading I"m afraid. Quote Link to comment https://forums.phpfreaks.com/topic/3315-php-5-method-overloading/#findComment-11308 Share on other sites More sharing options...
Barand Posted February 4, 2006 Share Posted February 4, 2006 You can have functions with optional arguments in PHP. Just specify a default if it is not provided.[code]function foo ($var = 1) { for ($i=0; $i < $var; $i++) echo "Hello world "}[/code]if you call with out the arg, it will default to "1" in this case[code]foo ();[/code]--> Hello world [code]foo (3);[/code]--> Hello world Hello world Hello world Quote Link to comment https://forums.phpfreaks.com/topic/3315-php-5-method-overloading/#findComment-11321 Share on other sites More sharing options...
daiwa Posted March 9, 2006 Share Posted March 9, 2006 [!--quoteo(post=342587:date=Feb 4 2006, 01:11 AM:name=gizmola)--][div class=\'quotetop\']QUOTE(gizmola @ Feb 4 2006, 01:11 AM) [snapback]342587[/snapback][/div][div class=\'quotemain\'][!--quotec--]Although Daiwa offers an interesting facility, I think it's pretty clear that using a switch() isn't a substitute for function overloading. In function overloading you could simply have a parameter with the same name, only a different datatype as a parameter, and that would be enough to differentiate an overloaded function. Since PHP really doesn't care about what type of parameter you pass to a function, it's just not a good platform for function overloading I"m afraid.[/quote]yes i was mentioning on the different numbers of parameters. what could be done to check the type of something is convert it to the type you want and then check the string representations to see if they are the same. meaning if i'm looking for ints. doing is_int() won't get you anywherebut if you convert it to int lets saywith settype or just casting depending on the situation and then compare the string representation of the old one with the new one if they do match then it was the type you were looking for. if not well then it wasn't (not this only works for ints really cause string well an int is a string but thats beyond the point here :P you can get around it but you shouldn't) Quote Link to comment https://forums.phpfreaks.com/topic/3315-php-5-method-overloading/#findComment-15796 Share on other sites More sharing options...
wickning1 Posted March 9, 2006 Share Posted March 9, 2006 You can achieve something close to overloading since parameters are optional. For instance, in your example, __construct() is unnecessary, because __construct($id) will accept both "new test()" and "new test(12)". $id will simply be unset in the first case.It's definitely not as flexible as true overloading, but like daiwa says, you can create a factory and pass it off based on the input you got. You can do type checking too with functions like is_numeric, is_array, is_object, and the language construct "instanceof" that checks if an object inherits a certain class (i.e. if ($inputobject instanceof myclass) {}) Quote Link to comment https://forums.phpfreaks.com/topic/3315-php-5-method-overloading/#findComment-15878 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.