Consider the following little class:
public static function foo()
Obviously, this works:
But this doesn't work:
$class = "myclass";
And this work-around is required:
$class = "myclass";
Geninune bug or intentional behaviour?
10-26-2009, 04:07 PM
Works as of PHP 5.3.0, did not work as of 5.2.9-2. Unknown if valid in 5.2.10 (and too lazy to dl).
Avoid the eval call and instead use: call_user_func(array($var, 'foo'));
btw, its actually documented as a new feature (I originally said bug, but it is specified as of 5.3.0):
Ah! I'm still using 5.2.0-8 (because I'm too lazy to DL 5.3!).
Why do you say avoid eval?
10-26-2009, 09:58 PM
... can introduce major security flaws unless used *very* carefully
... reduces readability of your script
... is harder to debug
... is unnecessary 99% of the times it's used
... is the illegitimate child of Satan himself
... some people claim is slower than the alternatives
Other than that, eval is great :D
As for PHP 5.3 - it's nice n all, but in my attempts to use it, I've had more headaches than anything. I'm told 5.3.1 is what the cool kids use. I use whatever cPanel supports (5.2.11).
10-27-2009, 12:22 AM
I've always heard if you feel the need to use eval in your code, take a step back from the computer for a few hours and re-think your logic :)
Ah! But eval is great for lazy people like me! :D
Seriously though, I do hardly ever use it because I do think it's ugly and potentially dangerous (e.g. code injection).
I tend to use singletons rather than static classes anyway.
10-27-2009, 11:06 AM
Eval's are only as dangerous as the developer leaves them, but with that said it does create a greater potential of oversight.
My issues with it are readability, speed and (as mentioned) the fact that eval is the son of satan. Eval is the only command that I almost never get to run on the first try. I don't know why.
Static elements/methods are not comparable to singleton classes. They are logically dissimilar. Static is used for any operation or data handled by a class, and independent of an instance. Static usages let you span the data across any instance of that class, while singletons prevent you from ever having more than one instance.