Page 1 of 1

function overloading?

Posted: Thu Mar 19, 2009 12:50 pm
by kbarrett2008
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

Re: function overloading?

Posted: Thu Mar 19, 2009 1:06 pm
by crazycoders
You can always overload parent class functions. Whatever they are...

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"
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.

Re: function overloading?

Posted: Thu Mar 19, 2009 1:12 pm
by kbarrett2008
ok, that makes sense.

THANKS!!