Page 1 of 1

Executing an include like a function

Posted: Sat Oct 10, 2009 7:57 pm
by JellyFish
I have these functions that I've placed in separate files under a directory called "/handlers". I want to execute these functions when I include them. I can't simply call the function within the file-to-be-included because I need a way to pass the arguments to the function.

/handler/oneOfMyHandlers.php

Code: Select all

 
function oneOfMyHandlers($arg1, $arg2)
{
//...
}
oneOfMyHandlers(); 
 
/someOtherFile.php

Code: Select all

 
include "$_SERVER[DOCUMENT_ROOT]/handler/oneOfMyHandlers.php";
 
As you can see there's no way to pass in arguments to the function. So what I need is a way to do pass in the arguments to the function. I could to this:

/someOtherFile.php

Code: Select all

 
$arg1 = "something";
$arg2 = "something elese";
include "$_SERVER[DOCUMENT_ROOT]/handler/oneOfMyHandlers.php";
 
/handlers/oneOfMyHandlers.php

Code: Select all

 
function oneOfMyHandlers($arg1, $arg2)
{
//...
}
oneOfMyHandlers($arg1, $arg2); 
 
But the only issue I have with this is that the $arg1 and $arg2 variables are hanging outside of the function call. I'm looking for something that's more compact. Something more like just calling a function. I guess I could just include the function and then do the calling outside of the included file:

/someOtherFile.php

Code: Select all

 
include "$_SERVER[DOCUMENT_ROOT]/handler/oneOfMyHandlers.php";
oneOfMyHandlers($someVariable, $someOtherVariable); 
 
/handlers/oneOfMyHandlers.php

Code: Select all

 
function oneOfMyHandlers($arg1, $arg2)
{
//...
}
 
The only other issue with this, that I haven't mentioned, is that the name of the function and file is in fact variable:

/someOtherFile.php

Code: Select all

 
include "$_SERVER[DOCUMENT_ROOT]/handler/".$handlerName.".php";
//How do I call the function who's name is $handlerName
 
I know if JavaScript I could do something like this:

Code: Select all

 
window[handlerName](arg1, arg2);
 
But how could I do something similar with PHP? I think that's the question I'm after.

Re: Executing an include like a function

Posted: Sat Oct 10, 2009 8:09 pm
by califdon
Include() or require() simply insert a file into another file. If the inserted file is a function, you just call it wherever you want to, with its arguments.

Re: Executing an include like a function

Posted: Sat Oct 10, 2009 8:36 pm
by JellyFish
califdon wrote:Include() or require() simply insert a file into another file. If the inserted file is a function, you just call it wherever you want to, with its arguments.
I know, but what I want to do is call an a function that exists in another file. The only issue is, I don't know the name of the function, it exists in a variable. How can I do this in PHP:

Code: Select all

 
window[functionName]();
 
???

Re: Executing an include like a function

Posted: Sun Oct 11, 2009 3:00 am
by requinix
So very, very close.

Code: Select all

include "$_SERVER[DOCUMENT_ROOT]/handler/".$handlerName.".php";
$handlerName($someVariable, $someOtherVariable);
They're called variable variables and variable functions.

Re: Executing an include like a function

Posted: Sun Oct 11, 2009 8:06 am
by jackpf
Surely you mean variable functions :P