473,386 Members | 1,745 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,386 software developers and data experts.

Overloaded functions and function pointers in PHP?

Hi all,

Since PHP is a (weakly) typed language, is it possible to overload functions
based on the type of their argument?

Also, are there function pointers in PHP?
Here's why I'm asking. Consider these two function:
function foo( $arg ) { echo "foo $arg foo\n"; }

function bar() { echo "barfoo"; }
I'd like to be able to do this:

foo('hello');
foo(&bar());

to produce this:

foo hello foo
foo barfoo foo

Is this at all possible (and convenient!) with PHP?

Thanks!
Pete
Sep 10 '05 #1
4 5356
Peter Salzman said the following on 10/09/2005 17:21:
Hi all,

Since PHP is a (weakly) typed language, is it possible to overload functions
based on the type of their argument?
No. But there are default arguments, and variable-length argument lists
(PHP 4+).
See http://www.php.net/manual/functions.arguments.php.

Also, are there function pointers in PHP?
No. But there are functions like call_user_func(), which make them
somewhat redundant.
See http://www.php.net/call_user_func.

Here's why I'm asking. Consider these two function:
function foo( $arg ) { echo "foo $arg foo\n"; }

function bar() { echo "barfoo"; }
I'd like to be able to do this:

foo('hello');
foo(&bar());

to produce this:

foo hello foo
foo barfoo foo


How about:

function foo($arg)
{
echo "foo $arg foo\n";
}

function bar()
{
return "barfoo";
}

foo(bar());
--
Oli
Sep 10 '05 #2
Oli Filth <ca***@olifilth.co.uk> wrote:
Peter Salzman said the following on 10/09/2005 17:21:

Here's why I'm asking. Consider these two function:
function foo( $arg ) { echo "foo $arg foo\n"; }

function bar() { echo "barfoo"; }
I'd like to be able to do this:

foo('hello');
foo(&bar());

to produce this:

foo hello foo
foo barfoo foo


How about:

function foo($arg)
{
echo "foo $arg foo\n";
}

function bar()
{
return "barfoo";
}

foo(bar());

The function argument seems to be executing before the call to foo is made.
In my own code, I'm getting the equivalent of:

barfoo foo foo

rather than

foo barfoo foo
Pete
Sep 10 '05 #3
Peter Salzman said the following on 10/09/2005 17:45:
Oli Filth <ca***@olifilth.co.uk> wrote:
Peter Salzman said the following on 10/09/2005 17:21:
Here's why I'm asking. Consider these two function:
function foo( $arg ) { echo "foo $arg foo\n"; }

function bar() { echo "barfoo"; }
I'd like to be able to do this:

foo('hello');
foo(&bar());

to produce this:

foo hello foo
foo barfoo foo


How about:

function foo($arg)
{
echo "foo $arg foo\n";
}

function bar()
{
return "barfoo";
}

foo(bar());


The function argument seems to be executing before the call to foo is made.
In my own code, I'm getting the equivalent of:

barfoo foo foo

rather than

foo barfoo foo


That's because your bar() is echoing; mine is returning.

--
Oli
Sep 10 '05 #4
Peter Salzman wrote:
Since PHP is a (weakly) typed language, is it possible to overload functions
based on the type of their argument?
Kind of. You can always check the type of argument that's actually
passed with one of the is_* function and have the function behave
differently (e.g. array vs. scalar).
Also, are there function pointers in PHP?
Kind of. In PHP, functions are referenced by name. If you do $func()
and the variable $func contains the name of a function, it will be
called.
I'd like to be able to do this:

foo('hello');
foo(&bar());

to produce this:

foo hello foo
foo barfoo foo


I don't think that's doable in any language.

Sep 10 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
2
by: desktop | last post by:
If a function should work with different types you normally overload it: void myfun(int a){ // do int stuff } void myfun(std::string str){ // do string stuff }
2
by: Marcin Gil | last post by:
First of all thanks for your patience and good pointers for Visitor and Curiously Recurring Template patterns last time :) Now another problem of mine. THE CODE #include <iostream>...
13
by: Tristan Wibberley | last post by:
Hi I've got implementing overloaded operator new and delete pretty much down. Just got to meet the alignment requirements of the class on which the operator is overloaded. But how does one...
9
by: Lawrence Krubner | last post by:
Possibly a dumb question but is type hinting allowed with overloaded methods? Can one class have these two methods? public function doSelect(Criteria $c) { // some code here } public...
5
by: Fokko Beekhof | last post by:
Hello all, please consider the following code: -------------------------------------------------- #include <tr1/memory> struct BaseA { int x;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.