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
7
  • also, possible duplicate of stackoverflow.com/questions/3223899/… accidentally flagged with the wrong link, this comment has the correct link :P Commented Jun 25, 2013 at 6: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). Commented Jun 25, 2013 at 6:17
  • also related: stackoverflow.com/questions/10085284/… Commented Jun 25, 2013 at 6: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 :| Commented Jun 25, 2013 at 6:23
  • 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. Commented Jun 25, 2013 at 6:33

1 Answer 1

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
)
Sign up to request clarification or add additional context in comments.

1 Comment

interesting approach. make sure to restore the original error handler though after creating the anonymous function. else you'll affect all subsequent error handling.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.