By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,985 Members | 2,884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,985 IT Pros & Developers. It's quick & easy.

Method/function overloading

P: n/a
Am I correct in thinking there is no method/function overloading of any
kind in any version of PHP?

Thanks,

Matthew
Dec 10 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On 10 Dec, 14:17, Matthew <matt...@spamkiller.comwrote:
Am I correct in thinking there is no method/function overloading of any
kind in any version of PHP?

Thanks,

Matthew
Depends if any of the about 758,000 items found by putting the words

overloading php

into Google fit the bill.
Dec 10 '07 #2

P: n/a
Captain Paralytic emailed this:
On 10 Dec, 14:17, Matthew <matt...@spamkiller.comwrote:
>Am I correct in thinking there is no method/function overloading of any
kind in any version of PHP?

Thanks,

Matthew

Depends if any of the about 758,000 items found by putting the words

overloading php

into Google fit the bill.
I did that before posting, and got hits for class, property, method, and
an extension for overloading. When I looked in manual I found a function
called overload() which will enable method and property overloading in PHP
versions 4 >= 4.2.0.

I can't seem to find a simple answer to my question, hence my post, which
is specific to methods/functions (including constructors). I was unable to
find an example of what I would call 'method overloading' that you would
use in C++, Java, C#, etc..

EG.

function DoThis()
{
//something
}
function DoThis($var1)
{
//something
}
function DoThis($var1, $var2)
{
//something
}

the method that runs is chosen simply on the basis of the number of arguments.

Thanks.
Dec 10 '07 #3

P: n/a
On Mon, 10 Dec 2007 15:17:56 +0100, Matthew <ma*****@spamkiller.comwrote:
Am I correct in thinking there is no method/function overloading of any
kind in any version of PHP?
RTFM:
http://nl2.php.net/manual/en/languag...verloading.php
--
Rik Wasmus
Dec 10 '07 #4

P: n/a
On 10 Dec, 14:56, Matthew <matt...@spamkiller.comwrote:
Captain Paralytic emailed this:
On 10 Dec, 14:17, Matthew <matt...@spamkiller.comwrote:
Am I correct in thinking there is no method/function overloading of any
kind in any version of PHP?
Thanks,
Matthew
Depends if any of the about 758,000 items found by putting the words
overloading php
into Google fit the bill.

I did that before posting, and got hits for class, property, method, and
an extension for overloading. When I looked in manual I found a function
called overload() which will enable method and property overloading in PHP
versions 4 >= 4.2.0.

I can't seem to find a simple answer to my question, hence my post,
Your question was whether you were right in "thinking there is no
method/function overloading of any kind in any version of PHP"

You have now said that "I found a function called overload() which
will enable method and property overloading"

So it seems pretty obvious to me that you already had the answer to
the question you asked.
Dec 10 '07 #5

P: n/a
Matthew wrote:
Am I correct in thinking there is no method/function overloading of any
kind in any version of PHP?

Thanks,

Matthew
Not in the way I think you're saying - where two methods in the same
class (or both not in any class) can have the same name but different
parameter lists . PHP hasn't gotten that far yet. Maybe in a future
release.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Dec 10 '07 #6

P: n/a
On Dec 10, 9:56 am, Matthew <matt...@spamkiller.comwrote:
Captain Paralytic emailed this:
On 10 Dec, 14:17, Matthew <matt...@spamkiller.comwrote:
Am I correct in thinking there is no method/function overloading of any
kind in any version of PHP?
Thanks,
Matthew
Depends if any of the about 758,000 items found by putting the words
overloading php
into Google fit the bill.

I did that before posting, and got hits for class, property, method, and
an extension for overloading. When I looked in manual I found a function
called overload() which will enable method and property overloading in PHP
versions 4 >= 4.2.0.

I can't seem to find a simple answer to my question, hence my post, which
is specific to methods/functions (including constructors). I was unable to
find an example of what I would call 'method overloading' that you would
use in C++, Java, C#, etc..

EG.

function DoThis()
{
//something}

function DoThis($var1)
{
//something}

function DoThis($var1, $var2)
{
//something

}

the method that runs is chosen simply on the basis of the number of arguments.

Thanks.
You can achieve the same thing by using default values for function
parameters:

function DoThis($var1 = 'foo', $var2 = 'bar')

If you don't provide values for those parameters it will take the
default instead.
Dec 10 '07 #7

P: n/a
Jerry Stuckle emailed this:
Matthew wrote:
>Am I correct in thinking there is no method/function overloading of
any kind in any version of PHP?

Thanks,

Matthew

Not in the way I think you're saying - where two methods in the same
class (or both not in any class) can have the same name but different
parameter lists . PHP hasn't gotten that far yet. Maybe in a future
release.
Thanks Jerry - this is what I meant and wanted to know.
Cheers.
Dec 10 '07 #8

P: n/a
ZeldorBlat emailed this:
On Dec 10, 9:56 am, Matthew <matt...@spamkiller.comwrote:
>Captain Paralytic emailed this:
>>On 10 Dec, 14:17, Matthew <matt...@spamkiller.comwrote:
Am I correct in thinking there is no method/function overloading of any
kind in any version of PHP?
Thanks,
Matthew
Depends if any of the about 758,000 items found by putting the words
overloading php
into Google fit the bill.
I did that before posting, and got hits for class, property, method, and
an extension for overloading. When I looked in manual I found a function
called overload() which will enable method and property overloading in PHP
versions 4 >= 4.2.0.

I can't seem to find a simple answer to my question, hence my post, which
is specific to methods/functions (including constructors). I was unable to
find an example of what I would call 'method overloading' that you would
use in C++, Java, C#, etc..

EG.

function DoThis()
{
//something}

function DoThis($var1)
{
//something}

function DoThis($var1, $var2)
{
//something

}

the method that runs is chosen simply on the basis of the number of arguments.

Thanks.

You can achieve the same thing by using default values for function
parameters:

function DoThis($var1 = 'foo', $var2 = 'bar')

If you don't provide values for those parameters it will take the
default instead.
It's not the same thing. But thanks for the hint.
Cheers.
Dec 10 '07 #9

P: n/a
On Dec 10, 11:40 am, Matthew <matt...@spamkiller.comwrote:
ZeldorBlat emailed this:
On Dec 10, 9:56 am, Matthew <matt...@spamkiller.comwrote:
Captain Paralytic emailed this:
>On 10 Dec, 14:17, Matthew <matt...@spamkiller.comwrote:
Am I correct in thinking there is no method/function overloading of any
kind in any version of PHP?
Thanks,
Matthew
Depends if any of the about 758,000 items found by putting the words
overloading php
into Google fit the bill.
I did that before posting, and got hits for class, property, method, and
an extension for overloading. When I looked in manual I found a function
called overload() which will enable method and property overloading in PHP
versions 4 >= 4.2.0.
I can't seem to find a simple answer to my question, hence my post, which
is specific to methods/functions (including constructors). I was unable to
find an example of what I would call 'method overloading' that you would
use in C++, Java, C#, etc..
EG.
function DoThis()
{
//something}
function DoThis($var1)
{
//something}
function DoThis($var1, $var2)
{
//something
}
the method that runs is chosen simply on the basis of the number of arguments.
Thanks.
You can achieve the same thing by using default values for function
parameters:
function DoThis($var1 = 'foo', $var2 = 'bar')
If you don't provide values for those parameters it will take the
default instead.

It's not the same thing. But thanks for the hint.
Cheers.
I didn't say it was the same. But you can achieve the same effect.
I've seen plenty of constructs like this:

function DoThis() {
return DoThis('foo');
}

function DoThis($var1) {
return DoThis($var1, 'bar');
}

functionDoThis($var1, $var2) {
//do something
}
In PHP the equivalent is:

function DoThis($var1 = 'foo', $var2 = 'bar') {
//do something
}
Dec 10 '07 #10

P: n/a
Matthew wrote:
Am I correct in thinking there is no method/function overloading of any
kind in any version of PHP?
OK. In some other programming language you might have:

function reverse (int $i)
{ return 0 - $i; }

function reverse (string $s)
{ return strrev($s); }

function reverse (array $a)
{ return array_reverse($a); }

In PHP you'd write:

function _reverse_int ($i)
{ return 0 - $i; }

function _reverse_string ($s)
{ return strrev($s); }

function _reverse_array ($a)
{ return array_reverse($a); }

function reverse ($x)
{
if (is_numeric($x))
return _reverse_int($x);
elseif (is_string($x))
return _reverse_string($x);
elseif (is_array($x))
return _reverse_array($x);
else
{
$emsg = sprintf("Can't reverse type '%s'",
gettype($x));
throw new Exception($emsg);
}
}

Different syntax, same effect.

Also, with regards to the number of arguments, consider:

function add_ints (int $a, int $b)
{ return $a + $b; }

function add_ints (int $a, int $b, int $c)
{ return $a + $b + $c; }

In PHP you could use a similar technique to what I showed before:

function _add_ints__2 ($a, $b)
{ return $a + $b; }

function _add_ints__3 ($a, $b, $c)
{ return $a + $b + $c; }

function add_ints ()
{
if (func_num_args()==2)
return call_user_func_array('_add_ints__2',
func_get_args());
elseif (func_num_args()==3)
return call_user_func_array('_add_ints__3',
func_get_args());
}

But it would be simpler to rewrite the function as:

function add_ints ($a=0, $b=0, $c=0)
{
return $a + $b + $c;
}

Or:

function add_ints ()
{
return array_reduce(
func_get_args(),
create_function('$a,$b', 'return $a+$b;')
);
}

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 3 days, 22:33.]

Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/
Dec 11 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.