Executing an include like a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Executing an include like a function

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Executing an include like a function

Post 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.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Executing an include like a function

Post 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]();
 
???
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Executing an include like a function

Post 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.
Last edited by requinix on Sun Oct 11, 2009 4:25 pm, edited 1 time in total.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Executing an include like a function

Post by jackpf »

Surely you mean variable functions :P
Post Reply