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

Posted on 2011-05-05
Last Modified: 2013-11-18
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.
Question by:handypam
    LVL 107

    Accepted Solution

    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.
    LVL 14

    Assisted Solution

    by:Scott Madeira
    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 that I modified for your situation.  survey.php would be the file you were checking the syntax of.

    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 '
    <hr />Last line of the output: ' . $last_line . '
    <hr />Return value: ' . $retval;

    Open in new window

    LVL 17

    Assisted Solution

    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?

    Just a thought.


    Author Comment

    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


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now