-1
 eval('class this {  ');

 eval('function this($l1,$l2) {
  if($l1==0) { throw new ErrorException("error"); }
  echo $l1.$l2;
  }}');

try {
 $t = new this(0,1);
}
catch(Exception $e) {
 echo $e->getMessage();
}

Why is this code not working?

Errors:

Parse error: syntax error, unexpected $end, expecting T_FUNCTION in .......(4) : eval()'d code on line 1

Parse error: syntax error, unexpected '}' in.......(9) : eval()'d code on line 4

Fatal error: Class 'this' not found in .......on line 12
user229044
  • 232,980
  • 40
  • 330
  • 338
mrN
  • 3,734
  • 15
  • 58
  • 82
  • 2
    The error is that use `eval`. It's not the error PHP is complaining about, but it's still the real error. –  Jan 05 '11 at 10:31
  • 2
    Any reason to use `eval` here? – zerkms Jan 05 '11 at 10:31
  • @zerkms... just learning – mrN Jan 05 '11 at 10:32
  • 2
    @mrNepal: it is not thing you should ever use, though. My opinion is that you're just wasting your time now. – zerkms Jan 05 '11 at 10:33
  • I'd say: Before you learn `eval` (or even learn about it), you should learn everything else in the language, very well, so you know how to solve that problems that seem to be solved "easily" by `eval` without it, i.e. better. –  Jan 05 '11 at 10:43
  • I asked about the error in my code, Does that deserve a `down vote`, I am not asking you whether eval should be used or not, so please keep your opinions to yourself, i asked a question and i would like if you answer it rather that lecturing me? – mrN Jan 05 '11 at 11:05
  • 1
    Such a command tone! What if I refuse to keep my opinion to myself? You gonna fire me? Why don't you like a lecture or two anyway? What's wrong in learning something useful? – Your Common Sense Jan 05 '11 at 11:12
  • @Col. Shrapnel... that comment was intended for the downvoter, I dont think I deserve a downvote for asking about the error at my code, no matter how much useless that piece of code is. As I have mentioned time to time her, I am also trying to learn something..... – mrN Jan 05 '11 at 11:42
  • Why you are so anxious for the vote? Does it matter anything? Still i see no reason to command enyone, even the downvoter. This is free site, and anyone can do anything as long as it fits the rules. – Your Common Sense Jan 05 '11 at 12:05
  • But to be fair, the downvote is imo not warranted. @mrNepal: See e.g. [Why exactly is eval evil?](http://stackoverflow.com/questions/2571401/why-exactly-is-eval-evil), although asked for Lisp, most applies to all languages (just ignore macros and add associative arrays/(hash)maps instead in the top answer's explanation why you don't need `eval`) –  Jan 05 '11 at 13:11
  • @Col. Shrapnel, being on free country does not mean going to a singing club and start dancing, does it? – mrN Jan 06 '11 at 11:48
  • Oh. You're taking a downvote THAT seriously? Poor boy – Your Common Sense Jan 06 '11 at 12:43
  • @mrNepal FYI, senseless profanity has no place here. – user229044 Jan 19 '11 at 19:01

3 Answers3

4

You can't eval() an unclosed statement like class this {. You'd have to put the entire block into one string and run the eval() once.

That said, there is probably no reason to use eval() in the first place. What are you trying to do?

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
2

eval accepts complete and valid piece of php code. class this { is not valid, bacause there is no } in the end.

zerkms
  • 249,484
  • 69
  • 436
  • 539
2

Like Pekka said, though you could append the code to eval() in a variable like so:

$textToEval = 'class this {  ';
$textToEval .= 'function this($l1,$l2) {
     if($l1==0) { throw new ErrorException("error"); }
     echo $l1.$l2;
     }}';
eval($textToEval);
user229044
  • 232,980
  • 40
  • 330
  • 338
SUDO Los Angeles
  • 1,555
  • 12
  • 10
  • 3
    Hush! Even though you are technically right, such information should at least be preceded by a large sermon on the evilness of `eval` unless it's clear that OP knows them and showed that its use is justified. –  Jan 05 '11 at 10:41