473,231 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

Method/function overloading

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
10 3453
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: iceColdFire | last post by:
Hi, What is the Diff btwn Function overloading and overriding thanks, a.a.cpp
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
3
by: Iyer, Prasad C | last post by:
I am new to python. I have few questions a. Is there something like function overloading in python? b. Can I overload __init__ method Thanks in advance regards
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
45
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
6
by: flopbucket | last post by:
Could someone explain to me what the difference is between function template specialization and function overloading? I guess overloading can change the number of parameters, but otherwise they...
15
by: lordkain | last post by:
is it possible to do some kind of function overloading in c? and that the return type is different
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.