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

address of functions

P: n/a

Is it possible to store the address or reference to a function in php? Maybe
using just a string and later dereferencing it?

e.g some ideas to illustrate what I mean

<?php
function myCompanyMotto(){
echo "hello";
}

$myarr["func1"]=&myCompanyMotto;
$myarr["func2"]="myCompanyMotto";

//how to get function from myarr and call it????

?>
--
Feb 20 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Rik
On Tue, 20 Feb 2007 22:53:14 +0100, Richard <rg****@gmail.comwrote:
>
Is it possible to store the address or reference to a function in php?
Maybe
using just a string and later dereferencing it?

e.g some ideas to illustrate what I mean

<?php
function myCompanyMotto(){
echo "hello";
}

$myarr["func1"]=&myCompanyMotto;
$myarr["func2"]="myCompanyMotto";

//how to get function from myarr and call it????

?>
I'd just use call_user_func()/call_user_func_array(), and save the
callback either as a string for normal functions, and as an array for
object/class methods.

function myCompanyMotto(){
echo "hello";
}
class foo{
function bar(){
echo 'class';
}
}
$myarr['func1'] = 'myCompanyMotto';
$myarr['func2'] = array('foo','bar');
call_user_func($myarr['func1']);
call_user_func($myarr['func2']);
--
Rik Wasmus
Feb 20 '07 #2

P: n/a
Rik <lu************@hotmail.comwrites:
On Tue, 20 Feb 2007 22:53:14 +0100, Richard <rg****@gmail.comwrote:
>>
Is it possible to store the address or reference to a function in
php? Maybe
using just a string and later dereferencing it?

e.g some ideas to illustrate what I mean

<?php
function myCompanyMotto(){
echo "hello";
}

$myarr["func1"]=&myCompanyMotto;
$myarr["func2"]="myCompanyMotto";

//how to get function from myarr and call it????

?>

I'd just use call_user_func()/call_user_func_array(), and save the
callback either as a string for normal functions, and as an array for
object/class methods.

function myCompanyMotto(){
echo "hello";
}
class foo{
function bar(){
echo 'class';
}
}
$myarr['func1'] = 'myCompanyMotto';
$myarr['func2'] = array('foo','bar');
call_user_func($myarr['func1']);
call_user_func($myarr['func2']);
Thanks, I had googled, but being new to PHP I hadnt found
call_user_func. Just what I want.
Feb 20 '07 #3

P: n/a
"Richard" <rg****@gmail.comwrote in message
news:87************@gmail.com...
>
Is it possible to store the address or reference to a function in php?
Maybe
using just a string and later dereferencing it?

e.g some ideas to illustrate what I mean

<?php
function myCompanyMotto(){
echo "hello";
}

$myarr["func1"]=&myCompanyMotto;
$myarr["func2"]="myCompanyMotto";

//how to get function from myarr and call it????

function foo(){
echo "Hello Kitty!";
}

$bar = 'foo';

$bar();
--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Feb 21 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.