• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

Is it possible to evaluate the code in an include file before it is included?

Hi there,


I have an application that generates code which it writes to file. Then the main program includes and runs this file in amongst all the other bits and bobs.
My question is ...

Is there a way to ensure that the generated code is without errors before it is run?
As on the odd occasion an error has been written into the file it just breaks the whole system.

I have been racking my brains to work out a way to do this but i cant think of anything.
I am not really looking for code, just a theory or idea as to how this could be achieved.

Thank you for your ideas.
0
handypam
Asked:
handypam
3 Solutions
 
Ray PaseurCommented:
A lot of the answer might depend on what the generated code is doing, data dependencies, etc.  You might consider having an asynchronous script test the code and return the output string.  I cannot do proof of concept, but since PHP will produce browser output you might be able to write the code to a file and send a link to a RESTFUL web service that loads the code and returns the browser output string.  You could find the error messages in this string, if any.

For obvious reasons you would want to protect this web service with an API key and maybe put it behind HTTPS.
0
 
Scott MadeiraCommented:
I assume you are talking about syntax errors and not logic errors.

I've never tried this but perhaps you could use command line php -l filename.php to check the syntax. Here is a code snippet I found on php.net that I modified for your situation.  survey.php would be the file you were checking the syntax of.

 
<?php
echo '<pre>';

// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('php -l survey.php', $retval);

// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>

Open in new window

0
 
psimationCommented:
Never tried it before myself, but maybe if you create a class that will "include" your code in a "try / catch" block you may be able to catch the error gracefully?

http://php.net/manual/en/language.exceptions.php

Just a thought.
 

0
 
handypamAuthor Commented:
thank you to everyone for your ideas.
I have implemented something along the lines of the first response and have therefore award it the most points.
But as all ideas have helped my thought process i am awarding all some points

thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now