Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts

    Genuine PHP bug or intentional behaviour?

    Consider the following little class:
    PHP Code:
    class myclass
    {
      public static function 
    foo()
      {
        echo 
    "bar\n";
      }

    Obviously, this works:
    PHP Code:
    myclass::foo(); 
    But this doesn't work:
    PHP Code:
    $class "myclass";
    $class::foo(); 
    And this work-around is required:
    PHP Code:
    $class "myclass";
    eval(
    $class."::foo();"); 
    Geninune bug or intentional behaviour?
    Die Welt ist ein Irrenhaus und hier ist die Zentrale!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Feature.
    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'));

    Edit:
    btw, its actually documented as a new feature (I originally said bug, but it is specified as of 5.3.0):
    http://www.php.net/manual/en/languag...ekudotayim.php
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Ah! I'm still using 5.2.0-8 (because I'm too lazy to DL 5.3!).

    Why do you say avoid eval?
    Die Welt ist ein Irrenhaus und hier ist die Zentrale!

  • #4
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Eval...

    ... 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

    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).
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #5
    Regular Coder FWDrew's Avatar
    Join Date
    Apr 2008
    Location
    Missouri
    Posts
    380
    Thanks
    38
    Thanked 45 Times in 43 Posts
    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
    Dev-Tips.com || Employee at the Envato Network || My Portfolio

  • #6
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Ah! But eval is great for lazy people like me!

    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.
    Die Welt ist ein Irrenhaus und hier ist die Zentrale!

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •