Picked up the book "PHP and MySQL Web Development" by Luke Welling and Laura Thomson.
I'm confused..
pg. 147 "Naming your function" -> Many languages do allow you to reuse function names. This feature is called function overloading. However, PHP does not support function overloading, ...
pg. 163 "Constructors" -> PHP supports function overloading, which means that you can provide more than one function with the same name and different numbers or types of parameters.
Ok.. so which is it? Can only constructors be overloaded? I have often overloaded "getters", so does this mean I can't do that? Don't see how PHP can claim to support OO without overloading.
So, what's the real story here?
Thanks in advance.
Ken
function overloading?
Moderator: General Moderators
-
crazycoders
- Forum Contributor
- Posts: 260
- Joined: Tue Oct 28, 2008 7:48 am
- Location: Montreal, Qc, Canada
Re: function overloading?
You can always overload parent class functions. Whatever they are...
For example:
So you can overload any function from a base class... But in the generic scope you cannot do that, only one function can exist of the same name at the same time.
Note: Namespaces are coming in the next release of PHP and will support several same names but in different namespaces. I don't know though if namespaces can be applied to global functions.
For example:
Code: Select all
class a {
public function foo(){
echo 'foo';
}
}
class b extends a {
public function foo(){
parent::foo();
echo ' bar';
}
}
$c = new a();
$c->foo(); //Outputs "foo"
$c = new b();
$c->foo(); //Outputs "foo bar"Note: Namespaces are coming in the next release of PHP and will support several same names but in different namespaces. I don't know though if namespaces can be applied to global functions.
-
kbarrett2008
- Forum Newbie
- Posts: 9
- Joined: Fri Mar 13, 2009 5:09 pm
Re: function overloading?
ok, that makes sense.
THANKS!!
THANKS!!