[PHP] call function as variable

2003-08-16 Thread adam
Hi!

I have folowing function which they are a member in a class.

function foo(){
something
}
function zoo(){
something else
}
and i have a array such:

$test = array(1=foo,2=zoo);

and i want to call the fuction foo() and zoo something like;

$object-$test[1]();

I have tried this this, it will not working. But is there any other way 
to do something like this!!

cheers

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] call function as variable

2003-08-16 Thread John W. Holmes
adam wrote:
Hi!

I have folowing function which they are a member in a class.

function foo(){
something
}
function zoo(){
something else
}
and i have a array such:

$test = array(1=foo,2=zoo);

and i want to call the fuction foo() and zoo something like;

$object-$test[1]();
Try:

{$object-$test[1]}();

--
---John Holmes...
Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/

PHP|Architect: A magazine for PHP Professionals  www.phparch.com





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] call function as variable

2003-08-16 Thread adam
I got this error:

Parse error: parse error, unexpected '{' in ...

Cheers

John W. Holmes wrote:

adam wrote:

Hi!

I have folowing function which they are a member in a class.

function foo(){
something
}
function zoo(){
something else
}
and i have a array such:

$test = array(1=foo,2=zoo);

and i want to call the fuction foo() and zoo something like;

$object-$test[1]();


Try:

{$object-$test[1]}();



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] call function as variable

2003-08-16 Thread David Otton
On Sat, 16 Aug 2003 07:58:27 +0200, you wrote:

I have folowing function which they are a member in a class.

function foo(){
something
}

function zoo(){
something else
}


and i have a array such:

$test = array(1=foo,2=zoo);

and i want to call the fuction foo() and zoo something like;

$object-$test[1]();

The fact that you say $object-$test suggests that foo() and zoo() are
methods of a class? The following code snippet contains just about every
mechanism for calling a method of a class that there is. The one you'll
probably want is

call_user_func (array ($C, 'B'), 'call 4');

?

/* Class A has method B */
class A {
function B ($s = None) {
echo (pinput : $s/p);
}
}

/* $C is an instance of A */
$C = new A ();

/* $D is an array of strings */
$D = array ('item 1', 'item 2', 'item 3', 'item 4');

/* invoke A::B */
A::B ('call 1');

/* invoke $C-B */
$C-B ('call 2');

/* invoke A::B via call_user_func() */
call_user_func (array ('A', 'B'), 'call 3');

/* invoke $C-B via call_user_func() */
call_user_func (array ($C, 'B'), 'call 4');

/* invoke A::B via call_user_func_array() */
call_user_func_array (array ('A', 'B'), array('call 5'));

/* invoke $C-B via call_user_func_array() */
call_user_func_array (array ($C, 'B'), array('call 6'));

/* apply A::B to $D via array_walk() */
array_walk ($D, array ('A', 'B'));

/* apply $C-B to $D via array_walk() */
array_walk ($D, array ($C, 'B'));

?


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] call function as variable

2003-08-16 Thread Michael Sims
On Sat, 16 Aug 2003 07:58:27 +0200, you wrote:

$test = array(1=foo,2=zoo);

and i want to call the fuction foo() and zoo something like;

$object-$test[1]();

I've never used this myself, but this should work:

call_user_func($object, $test[1]);

See:

http://www.php.net/manual/en/function.call-user-func.php

HTH

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] call function as variable

2003-08-16 Thread adam
Tanks a lot!
this is exactly that i want!!
Cheers

Michael Sims wrote:

On Sat, 16 Aug 2003 07:58:27 +0200, you wrote:


$test = array(1=foo,2=zoo);

and i want to call the fuction foo() and zoo something like;

$object-$test[1]();


I've never used this myself, but this should work:

call_user_func($object, $test[1]);

See:

http://www.php.net/manual/en/function.call-user-func.php

HTH


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] call function as variable

2003-08-16 Thread adam
Tanks a lot!
This was exactly that i wanted!
Cheers

David Otton wrote:

On Sat, 16 Aug 2003 07:58:27 +0200, you wrote:


I have folowing function which they are a member in a class.

function foo(){
something
}
function zoo(){
something else
}
and i have a array such:

$test = array(1=foo,2=zoo);

and i want to call the fuction foo() and zoo something like;

$object-$test[1]();


The fact that you say $object-$test suggests that foo() and zoo() are
methods of a class? The following code snippet contains just about every
mechanism for calling a method of a class that there is. The one you'll
probably want is
	call_user_func (array ($C, 'B'), 'call 4');

?

/* Class A has method B */
class A {
function B ($s = None) {
echo (pinput : $s/p);
}
}
/* $C is an instance of A */
$C = new A ();
/* $D is an array of strings */
$D = array ('item 1', 'item 2', 'item 3', 'item 4');
/* invoke A::B */
A::B ('call 1');
/* invoke $C-B */
$C-B ('call 2');
/* invoke A::B via call_user_func() */
call_user_func (array ('A', 'B'), 'call 3');
/* invoke $C-B via call_user_func() */
call_user_func (array ($C, 'B'), 'call 4');
/* invoke A::B via call_user_func_array() */
call_user_func_array (array ('A', 'B'), array('call 5'));
/* invoke $C-B via call_user_func_array() */
call_user_func_array (array ($C, 'B'), array('call 6'));
/* apply A::B to $D via array_walk() */
array_walk ($D, array ('A', 'B'));
/* apply $C-B to $D via array_walk() */
array_walk ($D, array ($C, 'B'));
?



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php