function to execute another PHP script?
Moderator: General Moderators
function to execute another PHP script?
Is there a function to execute another PHP script out-of-process?
What I'm working on, is a framework for meta-programming in PHP. So a simple include or require won't do - I need to be able to execute a PHP script "outside" the current process ... in a separate PHP environment - so that, if the generated meta-program fails, the main program can capture the error.
I've tried the Apache-specific virtual() function - but this runs inside the current environment ... if the sub-script fails, the main script fails with it, and either way, the sub-script can access and overwrite variables in the main script.
I've also tried file_get_contents() with an URL to the sub-script, to get it's output - this works fine as such, except that I have no way to detect if an error occurred in the subscript, as this will simply generate an error-message as HTML inside the output, the way PHP normally does.
I've tried custom error-handling as well, using include/require with output-buffering to capture the output ... to no avail - too many types of errors can't be handled, and some of the errors that can't be handled then cause the subscript to output an error into the output-buffer, which is then never displayed, as the script terminates.
What I need, is basically a way to execute a PHP script, get it's output, and an error-status flag telling me if there were errors or not. Is there such a thing?
What I'm working on, is a framework for meta-programming in PHP. So a simple include or require won't do - I need to be able to execute a PHP script "outside" the current process ... in a separate PHP environment - so that, if the generated meta-program fails, the main program can capture the error.
I've tried the Apache-specific virtual() function - but this runs inside the current environment ... if the sub-script fails, the main script fails with it, and either way, the sub-script can access and overwrite variables in the main script.
I've also tried file_get_contents() with an URL to the sub-script, to get it's output - this works fine as such, except that I have no way to detect if an error occurred in the subscript, as this will simply generate an error-message as HTML inside the output, the way PHP normally does.
I've tried custom error-handling as well, using include/require with output-buffering to capture the output ... to no avail - too many types of errors can't be handled, and some of the errors that can't be handled then cause the subscript to output an error into the output-buffer, which is then never displayed, as the script terminates.
What I need, is basically a way to execute a PHP script, get it's output, and an error-status flag telling me if there were errors or not. Is there such a thing?
You mean, just execute the PHP interpreter from the command-line? I thought about that, but I don't like it because of security and cross-platform issues.
I wish there was a command to execute another script and get it's output and/or return-code.
But as long as there's not, I suppose this solution will have to do. I need the return-code though, so I'll have to use proc_open() ...
I wish there was a command to execute another script and get it's output and/or return-code.
But as long as there's not, I suppose this solution will have to do. I need the return-code though, so I'll have to use proc_open() ...
The easiest way would be call the file using an http wrapper.
Code: Select all
$output = file_get_contents('http://www.domain.com/file.ext');bokehman: yes, that was my previous attempt at a solution.
unfortunately, this can't tell you the error-level, so you can't know if the script produced an error.
anyways, I've now made a solution that uses proc_open(), which works ... sort of.
I can get the error-level now. But then I have problems at the next level, where the generated script is supposed to be executed - warnings and notices do not raise an error-level, so only errors can be captured ... notices and warnings show up in the generated code, which of course is bad.
Is there a way to tell PHP to abort on ANY error, including warnings and notices?
unfortunately, this can't tell you the error-level, so you can't know if the script produced an error.
anyways, I've now made a solution that uses proc_open(), which works ... sort of.
I can get the error-level now. But then I have problems at the next level, where the generated script is supposed to be executed - warnings and notices do not raise an error-level, so only errors can be captured ... notices and warnings show up in the generated code, which of course is bad.
Is there a way to tell PHP to abort on ANY error, including warnings and notices?
Actually, that was the first thing I tried - I thought that would suffice. But it doesn't - compiler errors (and certain other errors) are never passed to the error-handler at all, PHP just cold dies.
But I suppose I could use an error-handler to capture warnings and notices, and die() to force the script to quit with an exit-code if a warning or notice occurs. Is that what you had in mind?
I suppose I'll give that a try... thanks
But I suppose I could use an error-handler to capture warnings and notices, and die() to force the script to quit with an exit-code if a warning or notice occurs. Is that what you had in mind?
I suppose I'll give that a try... thanks
Of course not! If there is a parse error the file cannot be read so php will never know about the error handler. One way to get around this is to write the error handler into a script, and then save it as your debug script. This file will then include the file to be tested. This means the error handler will be loaded before the include and will not be affected by parse errors etc.mindplay wrote:Actually, that was the first thing I tried - I thought that would suffice. But it doesn't - compiler errors (and certain other errors) are never passed to the error-handler at all, PHP just cold dies.
I dont' think that works in practice? I usually have my custom error-handlers in a separate script - it usually does not mean that my custom error handler will be called if an included script can not be compiled. But then I've only just started working with PHP5 - maybe this was only impossible in PHP4?bokehman wrote:One way to get around this is to write the error handler into a script, and then save it as your debug script. This file will then include the file to be tested. This means the error handler will be loaded before the include and will not be affected by parse errors etc.
If the error handler is compiled before the file is included any parse error in the included file will be dealt with by the custom handler.mindplay wrote:I dont' think that works in practice? I usually have my custom error-handlers in a separate script - it usually does not mean that my custom error handler will be called if an included script can not be compiled. But then I've only just started working with PHP5 - maybe this was only impossible in PHP4?bokehman wrote:One way to get around this is to write the error handler into a script, and then save it as your debug script. This file will then include the file to be tested. This means the error handler will be loaded before the include and will not be affected by parse errors etc.