Jump to content

php 5 method overloading


frustrated

Recommended Posts

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??
Link to comment
Share on other sites

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 factory


class test
{
private $id;

public function __construct()
{
$args = func_num_args();

switch($args){

case(1).... anyways you get the point
call construct from ID function
break
case (2) and so on
}

}


}
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 1 month later...
[!--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 anywhere

but if you convert it to int lets say
with 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)
Link to comment
Share on other sites

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) {})
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.