Link Posted February 24, 2008 Share Posted February 24, 2008 In PHP 5, how can I have class blah { function blah($var1, $var2) { } function blah() { } } ??? Quote Link to comment https://forums.phpfreaks.com/topic/92675-overloading-constructor/ Share on other sites More sharing options...
KrisNz Posted February 24, 2008 Share Posted February 24, 2008 The short answer is you can't. The long answer is you can produce a similar behavior using __call and checking the number of arguments before calling the correct method, but the methods still can't have the same name. Quote Link to comment https://forums.phpfreaks.com/topic/92675-overloading-constructor/#findComment-474911 Share on other sites More sharing options...
Stooney Posted February 24, 2008 Share Posted February 24, 2008 You can override a method of an extension of a class. Example: <?php class Animal{ $this->name='Fred'; public function eat(){ echo $this->name.' eats.'; } } class Dog extends Animal{ public function eat(){ echo $this->name.' enjoys his Kibbles n Bits'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/92675-overloading-constructor/#findComment-474936 Share on other sites More sharing options...
KrisNz Posted February 24, 2008 Share Posted February 24, 2008 method overriding is not the same as method overloading. Since you want to do this with a constructor it would look something like <?php class Foo { public function __construct() { $all_args = func_get_args(); if (count($all_args) == 1) { $this->construct_one($all_args[0]); } else { $this->construct_two($all_args[0],$all_args[1]); } } protected function construct_one($a) { echo ($a); } protected function construct_two($a,$b) { echo ($a); echo ($b); } } <? Obviously, that's not as cool. Quote Link to comment https://forums.phpfreaks.com/topic/92675-overloading-constructor/#findComment-474983 Share on other sites More sharing options...
Daniel0 Posted February 24, 2008 Share Posted February 24, 2008 Just use one method, set the default argument values to null and check inside the method whether they're set or not. Quote Link to comment https://forums.phpfreaks.com/topic/92675-overloading-constructor/#findComment-474984 Share on other sites More sharing options...
Mastodont Posted February 24, 2008 Share Posted February 24, 2008 Have a look at functions func_num_args and func_get_args. You need not to define any arguments, just use them. Quote Link to comment https://forums.phpfreaks.com/topic/92675-overloading-constructor/#findComment-474986 Share on other sites More sharing options...
aschk Posted February 25, 2008 Share Posted February 25, 2008 Actually have a look at the Reflection class that comes with PHP5.1 now http://uk.php.net/oop5.reflection I think there are several functions there you will be able to use to do method overloading (sort of). Otherwise, just don't do it. Overloading is apparent in C++/C# and Java I know, but is it necessarily a good thing? Name your functions by what they're going to do and if you're expecting a variable number of parameters considering using a class helper object or an array (or worst of all default param values). Quote Link to comment https://forums.phpfreaks.com/topic/92675-overloading-constructor/#findComment-475680 Share on other sites More sharing options...
Daniel0 Posted February 25, 2008 Share Posted February 25, 2008 It is a good think. It'll enable you to make polymorphic methods so you can create an interface which will act accordingly to the types given. Take for instance the + operator in a strictly typed language. If it wasn't overloaded then you'd need four operators instead of one (if we only take integer and float types into account): int + int, int + float, float + int and float + float. In some many languages it acts as a concatenation operator as well which will require additional overloading again. The reflection classes can however not add that type of overloading functionality. You'll have to use the magic __call() method for a kind of emulated overloading. Quote Link to comment https://forums.phpfreaks.com/topic/92675-overloading-constructor/#findComment-475851 Share on other sites More sharing options...
deadimp Posted February 25, 2008 Share Posted February 25, 2008 To specify over what aschk and Daniel0, you can use the Reflection API to instantiate your class using a varying number of arguments as you would with call_user_func(). I'll leave it up to you to find which function that is in the manual. And backing what Daniel0 said, overloading is convenient for a strictly-typed language. You should have some sort of an idea about which type your variable is that you're handling, therefore you should know what you're doing with your functions, be they operators or the general kind. Quote Link to comment https://forums.phpfreaks.com/topic/92675-overloading-constructor/#findComment-476113 Share on other sites More sharing options...
deadonarrival Posted March 6, 2008 Share Posted March 6, 2008 Just make <?php public function doblah($var) { //Code common to blah1 and blah2 } blah1($arg) { //code specific to one argument doblah($arg); } blah2($arg,$thing) { //code specific to two arguements doblah($arg); } ?> Why does it all have to be one function? http://www.phpfreaks.com/forums/index.php/topic,186075.0.html Quote Link to comment https://forums.phpfreaks.com/topic/92675-overloading-constructor/#findComment-485502 Share on other sites More sharing options...
Daniel0 Posted March 6, 2008 Share Posted March 6, 2008 Because it's not the same. Try to take a look at overloading polymorphism or at the very least my post above. Quote Link to comment https://forums.phpfreaks.com/topic/92675-overloading-constructor/#findComment-485520 Share on other sites More sharing options...
deadonarrival Posted March 6, 2008 Share Posted March 6, 2008 I understand the concept of polymorphism, I just think it goes against the idea of not re-typing code. There are a very few circumstances I can see ever needing it, but how often do you need two functions with the same name? Two seperate functions could surely do in 99% of the cases? Either with a third function of "shared code" (otherwise the functions are using the same code, twice the typing if you need to change anything) or they're so different they should be seperate. Quote Link to comment https://forums.phpfreaks.com/topic/92675-overloading-constructor/#findComment-485522 Share on other sites More sharing options...
Jenk Posted March 11, 2008 Share Posted March 11, 2008 class B extends A { public function A () { // leave this blank to stop parent constructor executing if it's a php4 syntax class } public function __construct () { //this is your new constructor } } Quote Link to comment https://forums.phpfreaks.com/topic/92675-overloading-constructor/#findComment-489394 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.