6

I have this simple PHP code:

<?php

$code = "echo 'Hello World'; }";
call_user_func(create_function('', $code));

As you see, my $code has syntax error. When I run this, I get this result:

Parse error: syntax error, unexpected '}' in file.php(4) : runtime-created function on line 1
Warning: call_user_func() expects parameter 1 to be a valid callback, no array or string given in file.php on line 4

How can I get the Parse error into a variable? For example:

$error = some_func_to_get_error();
echo $error;
// Parse error: syntax error, unexpected '}' in file.php(4) : runtime-created function on line 1
user2480690
  • 155
  • 2
  • 10
  • also, possible duplicate of http://stackoverflow.com/questions/3223899/php-eval-and-capturing-errors-as-much-as-possible accidentally flagged with the wrong link, this comment has the correct link :P – Dave Chen Jun 25 '13 at 06:09
  • @DaveChen Thanks for the links, but it didn't give me complete result: **Line Number** and the **error itself**, it just give me **syntax error**. Also I get a **Warning** when running the function itself (on usage of `preg_match`). – user2480690 Jun 25 '13 at 06:17
  • also related: http://stackoverflow.com/questions/10085284/how-to-handle-parse-error-for-eval-function-in-php?rq=1 – Gordon Jun 25 '13 at 06:17
  • @Gordon I tested `eval` and can get `true` or `false` result. It's **good**, but I want **more** details about errors in the code. I think it's useful, but it seems that this doesn't done easily in PHP :| – user2480690 Jun 25 '13 at 06:23
  • @DaveChen `error_get_last()` on the above code will return the error of `call_user_func()` function, not the error of the anonymous function it try to execute. – user2480690 Jun 25 '13 at 06:26
  • Oops, I mean `set_error_handler` -- or `try...catch()`. – Dave Chen Jun 25 '13 at 06:29
  • 1
    @user2480690 the point of linking to that was to show you that there is no easy built-in way ;) the sanest choice would be to use ob like in Nikic's answer or write to a temp file and lint the file. – Gordon Jun 25 '13 at 06:33

1 Answers1

3

I envolved with this problem about a week and at last, I found something interesting. As you know, there is a build-it function called error_get_last which return the information about last error. In this code:

<?php

$code = "echo 'Hello World'; }";
call_user_func(create_function('', $code));
$error = error_get_last();

It will return something like this:

Array
(
    [type] => 2
    [message] => call_user_func() expects parameter 1 to be a valid callback, no array or string given
    [file] => file.php
    [line] => 4
)

The last error occured when executing call_user_func. It needs a callback, but, the create_function didn't work correctly (as $code has parse error).

But when setting a custom error handler which return true;, so the call_user_func won't throw any error, and the last error, would be the error within the runtime-created function.

<?php

// returning true inside the callback
set_error_handler(function () { return true; });

$code = "echo 'Hello World'; }";
call_user_func(create_function('', $code));
$error = error_get_last();

And now the error would be like this:

Array
(
    [type] => 4
    [message] => syntax error, unexpected '}'
    [file] => file.php(7) : runtime-created function
    [line] => 1
)
user2480690
  • 155
  • 2
  • 10
  • 2
    interesting approach. make sure to restore the original error handler though after creating the anonymous function. else you'll affect all subsequent error handling. – Gordon Jun 25 '13 at 11:28