is method overloading possible in php?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

is method overloading possible in php?

Post by saumya »

Hi guys,
Is it possible to overload a method in php like in java? I mean how to make a method overloaded?
thanking youi
saumya
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post by saumya »

thanks a lot.
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

What is called "overloading" in PHP is significantly different from what overloading means in most other languages. Overloading typically means having different signatures for the same named method (or function) and having those different signature react appropriatly. This often worked off of type hints (type fatals actually ;) in php ) in statically typed languages. In PHP, you would have to encode all of the possible signatures and behaviors within the same function body. You have a lot of flexibility: default parameters, checking parameter types (is_numeric, is_string, is_object), checking class (is_a, instance_of, get_class), duck typing, and allowing for variable parameter lists (funct_num_args, funct_get_args).

HTH
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post by saumya »

thank you.
Yes,you are right.
this is the thing i got to know, when i read the article given by patrikG.
here overloading does something else. One has to write all the methods to deal with different type of datas and number of datas manually.WEll, no problem.I was just seeking the info. Now i know i have to write it manually, so ai will go about it calmly.Or else i would have been searching for how to write an overloaded method.
thanking you
saumya
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Yeah, I learned php before C++, so C++ always bugs me that you actually have to write a function twice. ^^;
Post Reply