Hi Rasmus.

Thanks for the help.
I've implemented your code in a method in class 'RendererParam' as follows:
//--------------------------------------------------------------------------
-----------------------------------
// evaluateParameter
//--------------------------------------------------------------------------
-----------------------------------
    /**
    Evaluate the parameter's code.
    */
    function evaluateParameter ( ) {
        // Give variable containing error message global scope.
        global $php_errormsg;
        echo "Evaluate code...<BR>";
        // Supress error reporting.
        error_reporting( 0 );
        // Set 'track_errors' on.
        ini_set( 'track_errors', true );
        // Clean out error variable.
        $php_errormsg = "";
        // Start output buffering.
        echo "eval( $this->sParamName.' = '.$this->sParamValue.';' )<BR>";
        ob_start( );
        // Evaluate this parameter.
        eval( $this->sParamName.' = '.$this->sParamValue.';' );
        // Get the output buffer contents.
        $sBufferOutput = ob_get_contents( );
        // End output buffering.
        ob_end_clean( );
        echo '$sBufferOutput = '.$sBufferOutput.'<BR>';
        if ( trim( $php_errormsg ) != '' ) {
            echo "Error was: $php_errormsg<br>\n";
            return false;
        }
        else {
            echo "Output was: $sBufferOutput<br>\n";
            return true;
        }
    }
//--------------------------------------------------------------------------
-----------------------------------
Example output:

Evaluate code...
eval( $AUTHOR.' = '.$oArticle->oAuthor->getName( ).';' )

This works greate for parse errors, which is all I really want to check for.
But I have picked up a problem.
As the parameters (read variables ) & their values entered by the user may
not be in scope at the time they are entered,
in the above case I would get a fatal error:

    Fatal error: Call to a member function on a non-object
    in /var/www/vne/vne_classes/entity/RendererParam.inc(350) : eva()'d
    code on line 1

This is most obviously because I am trying to call a method of an object
which does not exist
If I understand correctly, 'error_reporting( 0 );' will supress error
messages, but this fatal error will still cause
the php preprocessor to die.

Doing a 'global $$this->sParamName, $$this->sParamValue;' will not help
either, as these given variables/objects
will not necessarly be available in the scope outside of this method either.

So...
Is there any way of telling the parser to ignore this fatal error and carry
on, or alernatively,
a way of parsing a string at face value for parse errors ie. 'Look for
syntax errors, but don't evaluate the code.'?

Thanks again for the help, it is much appreciated

Regards

Scott

----- Original Message -----
From: "Rasmus Lerdorf" <[EMAIL PROTECTED]>
To: "Scott Houseman" <[EMAIL PROTECTED]>
Cc: "php-general" <[EMAIL PROTECTED]>
Sent: Wednesday, April 03, 2002 10:33 AM
Subject: Re: [PHP] Evaluating php code.


> > Ideally, i'd like to evaluate the code the user has submitted, and if an
> > error is generated, notify the user of that fact.
> > Eval always returns false, and I'd like no runtime error to be
generated.
> > Perhaps an error-handler is what's needed?
> >
> > What can you suggest?
>
> I think this should illustrate how to do that:
>
> <?
>     $code = '
>
> <?
> $a = 1;
> print $a;
> ?>
>
> ';
>
>     error_reporting(0);
>     ini_set('track_errors',true);
>     $php_errrormsg='';
>     ob_start();
>     eval('?>'.$code);
>     $output = ob_get_contents();
>     ob_end_clean();
>     if($php_errormsg) echo "Error was: $php_errormsg<br>\n";
>     else echo "Output was: $output<br>\n";
> ?>
>
> A couple of tricks:
>
> 1. Turning off PHP's error_reporting makes sure no errors are shown by PHP
> 2. Turning on track_errors puts any errors into the $php_errormsg variable
> 3. Turning on output buffering lets you catch the output from the code you
>    are testing
> 4. Preceding the eval()'ed code with ?> makes sure you start the code off
>    in normal HTML mode since eval() actually assumes what you feed it
>    starts in PHP mode which is likely not the case for you.
>
> -Rasmus
>
>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to