Jump to content


Photo

php 5 method overloading


  • Please log in to reply
6 replies to this topic

#1 frustrated

frustrated
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 03 February 2006 - 08:14 PM

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)

class test
 {
  private $id;
  
  public function __construct()
   {
   }

  public function __construct($id)
   {  
    $this->id = $id;
   }
 }

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??

#2 daiwa

daiwa
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 04 February 2006 - 04:56 AM

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
}

}


}

#3 frustrated

frustrated
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 04 February 2006 - 05:16 AM

[!--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.

#4 gizmola

gizmola
  • Administrators
  • Advanced Member
  • 4,664 posts
  • LocationLos Angeles, CA USA

Posted 04 February 2006 - 06:11 AM

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.

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 04 February 2006 - 07:01 PM

You can have functions with optional arguments in PHP. Just specify a default if it is not provided.


function foo ($var = 1) {

        for ($i=0; $i < $var; $i++) echo "Hello world "
}

if you call with out the arg, it will default to "1" in this case

foo ();
--> Hello world

foo (3);
--> Hello world Hello world Hello world

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 daiwa

daiwa
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 09 March 2006 - 03:29 PM

[!--quoteo(post=342587:date=Feb 4 2006, 01:11 AM:name=gizmola)--][div class=\'quotetop\']QUOTE(gizmola @ Feb 4 2006, 01:11 AM) View Post[/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)

#7 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 09 March 2006 - 07:42 PM

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) {})




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users