SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Miami, Florida - Caracas, Venezuela
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    eval() in a class

    does anyone have a clue as to why this won't work:

    PHP Code:
      class Dispatcher{
            function 
    object_content($object_id){
                return eval(
    "$this->test_output($object_id);");
            }
          function 
    test_output($object_id){
              return 
    $object_id;
          }
      } 
    this is how I'm calling it:
    PHP Code:
       $dispatch=new Dispatcher;
       print 
    $dispatch->object_content(2000); 
    Luis

  2. #2
    SitePoint Addict timvw's Avatar
    Join Date
    Jan 2005
    Location
    Belgium
    Posts
    354
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what doesn't work? what do you expect from this code?

  3. #3
    Free Geek computerages's Avatar
    Join Date
    Oct 2004
    Location
    /dev/null
    Posts
    1,071
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool

    Try changing this code
    PHP Code:
    class Dispatcher{
            function 
    object_content($object_id){
                return eval(
    "$this->test_output($object_id);");
            }
         function 
    test_output($object_id){
             return 
    $object_id;
         }

    to this
    PHP Code:
    class Dispatcher{
            function 
    object_content($object_id){
                return eval(
    $this->test_output($object_id));
            }
         function 
    test_output($object_id){
             return 
    $object_id;
         }

    I don't know what you are expecting from this as timvw mentioned, just give that a try!

  4. #4
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Miami, Florida - Caracas, Venezuela
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the idea is to make a call to method "object_content" that calls another method whos name is dynamic. So the call to eval that looks like this
    PHP Code:
                   function object_content($object_id){
                       
    //based on $object_id I get $dynamic_method_name so then I can call:
                       
    return eval("$this->".$dynamic_method_name."($object_id);");
                   } 
    so if $dynamic_method_name is "test_output" I would be doing:

    PHP Code:
          function object_content($object_id){
                      return eval(
    "$this->test_output($object_id);");
                  } 
    which I'd like to output the same as if I was doing:

    PHP Code:
          function object_content($object_id){
                      return 
    $this->test_output($object_id);
                  } 
    but instead does nothing.
    Luis

  5. #5
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Miami, Florida - Caracas, Venezuela
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    computerages:

    what you did there is what I want in theory but the method passed to eval should be built based on a string.
    Luis

  6. #6
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    Finland
    Posts
    703
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHP Manual
    In PHP 4, eval() returns NULL unless return is called in the evaluated code, in which case the value passed to return is returned.
    PHP Code:
    function object_content($object_id){
                      return eval(
    "return $this->test_output($object_id);");
                  } 

  7. #7
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Miami, Florida - Caracas, Venezuela
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You wouldn't believe me if I told you I read the manual and didn't realize the return should also be included as part of the evaluated strin. Would you?

    You are a genius!!

    Thank you so much.
    Luis

  8. #8
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Melbourne
    Posts
    362
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, be careful to escape the $ inside the double quotes, otherwise it can be parsed out before the call to eval is made.

    Regardless, why don't you do something like

    PHP Code:
    $func 'test_output';
    $value $this->$func(); 
    Seems a lot easier to me.

  9. #9
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't you need:
    return eval('$this->test_output('.$object_id.');');

    Or (if you insist on using double quotes):
    return eval("\$this->test_output($object_id);");

    ? I've never used eval before, just guessing...

    Also, in a case like this, I'd personally just use Cal_User_func, but that's just me. Your choice.

  10. #10
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Eval is evil. Don't do it.

    If you really have to you'll know to ignore that. Trouble is, if you don't really have to, but think you do, you'll ignore it anyway. So, do you really have to..?

  11. #11
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    I thought it was because eval isnt really a function.
    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users

  12. #12
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    New Jersey
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think that your logic may be flawed, what your code shows you doing is essentially,
    PHP Code:
    class Dispatcher2 {
        function 
    object_content($object_id){
            return 
    $this->test_output($object_id);
        }
        function 
    test_output($object_id){
            return 
    $object_id;
        }
    }

    $test = new Dispatcher2();
    echo 
    $test->object_content(2000); 
    However when you explain what you want to do, you seem like you want something like,
    PHP Code:
    class Dispatcher {
        function 
    foo($a) {
            return 
    $this->$a();
        }

        function 
    bar() {
            return 
    'test';
        }
    }

    $test = new Dispatcher();
    echo 
    $test->foo('bar'); 
    Neither of these things require the use of eval() and as McGruff says,
    Quote Originally Posted by McGruff
    Eval is evil. Don't do it.

  13. #13
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lgomez
    the idea is to make a call to method "object_content" that calls another method whos name is dynamic.
    You want the object flavor of call_user_func...():

    PHP Code:
      class Whatever
     
    {
      function 
    compute_method_name($selector)
      {
          return 
    'method_'$selector;
      }
      
      function 
    object_content($selector$param)
      {
         
    $method_name $this->compute_method_name($selector);
      
          return 
    call_user_func(array(&$this$method_name),
              
    $param);
      }
      
      function 
    method_xxx($param)
      {
          return 
    'xxx';
      }
      
      function 
    method_yyy($param)
      {
          return 
    'yyy';
      }
     } 
    =Austin

  14. #14
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Eval is evil. Don't do it.
    Enlightening. I did not know that there were statements in PHP that were created by demonic forces. Perhaps something like "You cannot gain entry into Heaven if you have ever used eval" might be a better slogan to dissuade people. And why is it evil -- because it is.

    Did anyone ever notice that if you change the a in eval to an i . . .
    Christopher

  15. #15
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Miaaow.

    Eval is a smell. How often do you really need run-time code generation? Often it's an ugly hack and you could have solved the problem in a better way. Depends on the context.

  16. #16
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How often do you really need run-time code generation?
    Not very often, but when you do sometimes eval is a good fit. (like here)
    Often it's an ugly hack and you could have solved the problem in a better way. Depends on the context.
    That is actually the best general description of programming I have ever heard. Excellent!
    Christopher


Bookmarks

Posting Permissions

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