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:
But how could I do something similar with PHP? I think that's the question I'm after.