Calling functions which names are inside a variable

This is a discussion on Calling functions which names are inside a variable within the PHP General forums, part of the PHP Programming Forums category; On Sat, 2007-08-25 at 23:46 -0600, Robert Keizer wrote: > I am currently working on a module ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-26-2007
Robert Cummings
 
Posts: n/a
Default Re: [PHP] Calling functions which names are inside a variable

On Sat, 2007-08-25 at 23:46 -0600, Robert Keizer wrote:
> I am currently working on a module testing class, I can't seem to find the
> correct syntax. Here is an example of the problem:
>
> function foo( $var ){
> include $var.'.php';
> return $var(); // <!------- problem
> };
>
> foo("somefunction");
>
> In other words I am looking for a way to call a function by the value of a
> variable. I get a Call to undefined method error,
> any help would be great..


You are calling the function correctly when using a variable. Namely:

$var();

Check that the include file you want included is actually being
included. My guess is that it is not, or the function you expect to be
defined in it is not actually defined there.

Cheers,
Rob.
--
.................................................. ..........
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
.................................................. ..........
Reply With Quote
  #2 (permalink)  
Old 08-26-2007
Robert Keizer
 
Posts: n/a
Default Calling functions which names are inside a variable

I am currently working on a module testing class, I can't seem to find the
correct syntax. Here is an example of the problem:

function foo( $var ){
include $var.'.php';
return $var(); // <!------- problem
};

foo("somefunction");

In other words I am looking for a way to call a function by the value of a
variable. I get a Call to undefined method error,
any help would be great..

--
nuclearsanta@gmail.com
Reply With Quote
  #3 (permalink)  
Old 08-26-2007
Robert Keizer
 
Posts: n/a
Default Re: Calling functions which names are inside a variable

Thats great thanks, but it doesn't solve my problem completly, im trying to
use $output = $var(); .. which gives me an error. The $var(); works though,
so thanks for that.

Robert Keizer
""Robert Keizer"" <nuclearsanta@gmail.com> wrote in message
news:op.txnjmzww9011uo@lenovo-b0c37194...
>I am currently working on a module testing class, I can't seem to find the
>correct syntax. Here is an example of the problem:
>
> function foo( $var ){
> include $var.'.php';
> return $var(); // <!------- problem
> };
>
> foo("somefunction");
>
> In other words I am looking for a way to call a function by the value of a
> variable. I get a Call to undefined method error,
> any help would be great..
>
> --
> nuclearsanta@gmail.com

Reply With Quote
  #4 (permalink)  
Old 08-26-2007
Sanjeev N
 
Posts: n/a
Default RE: [PHP] Calling functions which names are inside a variable

I tested this functionality.

This is working fine.



foo.php

<?

function foo( $var ){

include $var.'.php';

return $var();

};



foo("somefunction");

?>

somefunction.php

<?

function somefunction(){

echo "hello";

}

?>

Out put is hello.



You make sure that your both files are in same folder and make sure that
$var.'.php' is exist with $var function.



Warm Regards,

Sanjeev

http://www.sanchanworld.com/

http://webdirectory.sanchanworld.com - Submit your website URL

http://webhosting.sanchanworld.com - Choose your best web hosting plan

-----Original Message-----
From: Robert Keizer [mailto:nuclearsanta@gmail.com]
Sent: Sunday, August 26, 2007 11:16 AM
To: php-general@lists.php.net
Subject: [php] Calling functions which names are inside a variable



I am currently working on a module testing class, I can't seem to find the

correct syntax. Here is an example of the problem:



function foo( $var ){

include $var.'.php';

return $var(); // <!------- problem

};



foo("somefunction");



In other words I am looking for a way to call a function by the value of a

variable. I get a Call to undefined method error,

any help would be great..



--

nuclearsanta@gmail.com



--

PHP General Mailing List (http://www.php.net/)

To unsubscribe, visit: http://www.php.net/unsub.php


Reply With Quote
  #5 (permalink)  
Old 08-26-2007
Richard Lynch
 
Posts: n/a
Default Re: [PHP] Calling functions which names are inside a variable

On Sun, August 26, 2007 12:46 am, Robert Keizer wrote:
> I am currently working on a module testing class, I can't seem to find
> the
> correct syntax. Here is an example of the problem:
>
> function foo( $var ){
> include $var.'.php';
> return $var(); // <!------- problem


Try this:
return ($var());

Not sure it's the answer, but suspect it might be.

And 'somefunction' *IS* defined, right?...

> };


--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:01 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0