How to include only the functions from a php file? [duplicate]
Possible Duplicate:
Including PHP functions from another file
I'm building code that us开发者_Go百科e function calls that exist in a thirdparty php file. This 3p php file is not a function library but also contain inline code itself.
How do I call the functions without executing the inline code?
edit: duplicate of Including PHP functions from another file ?
How do I call the functions without executing the inline code?
You can't.
You might be able to suppress the includes' output using output buffering, but the code will still execute.
I don't think there is a good way to do this without making copies of the respective includes, and stripping the inline code from them.
The simple answer is you can't. A call to include
is simply opening the file and running eval()
on the content.
How about splitting the original file into a functions-file and a native code file and including them? This way the original file will work as before and you would be able to include just the functions in your script.
Theoretically it is possible with evaling of filtered content of file, but filtering code is not an easy task in terms of scripting and performance.
If all your functions are defined at the top of the file you want to include, you can add a structure like this to it immediately after the function definitions:
if (something that would be true if the file was included) {
return;
}
For example, if the script doing the include has already defined a variable called $myVar
you could do:
if (isset($myVar)) return;
This will cause the included file to stop executing and return control to your original script.
However, the best solution is to create another file called e.g. 'functions.php' and include('functions.php');
in both scripts...
精彩评论