Help with eval()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Powell

    #1

    Help with eval()

    I have this line in my code:

    eval('$field->' . $val . '(' . $max . ',' . $errDisplayArra y[$key] .
    ');');

    where $field is a predefined class instance, $val is a string
    containing the name of an existing method within the class, and
    $errDisplayArra y[$key] will be an array element containing text that
    goes appropriately into the class method whose name is in $val.

    Whenever I run this I get a parse error, however, upon using a die()
    statement instead of eval it configures as so (and works!):

    $field->setMaxLength(4 0,"The last name cannot be more than 40
    characters");

    when I insert this line verbatim into my code I receive no breakage of
    any kind upon that line. I have read the eval() statement all over
    php.net, phpbuilder, phpfreaks and zend and I can't see what I'm doing
    wrong. Could someone clarify it for me?

    Thanx
    Phil
  • Andy Hassall

    #2
    Re: Help with eval()

    On 9 Oct 2003 14:33:46 -0700, soazine@erols.c om (Phil Powell) wrote:
    [color=blue]
    >I have this line in my code:
    >
    >eval('$field->' . $val . '(' . $max . ',' . $errDisplayArra y[$key] .
    >');');
    >
    >where $field is a predefined class instance, $val is a string
    >containing the name of an existing method within the class, and
    >$errDisplayArr ay[$key] will be an array element containing text that
    >goes appropriately into the class method whose name is in $val.[/color]

    First question would be: what are the actual values of each of those
    variables? Echo them out and post them please.

    Then construct the string you're going to eval() and put it in another
    variable; echo that out, to make sure it's all exactly as you wanted.
    [color=blue]
    >Whenever I run this I get a parse error, however, upon using a die()
    >statement instead of eval it configures as so (and works!):
    >
    >$field->setMaxLength(4 0,"The last name cannot be more than 40
    >characters") ;[/color]

    Ah, you're saying that's the output from die()? OK.

    So exactly what sort of parse error do you get from it?
    [color=blue]
    >when I insert this line verbatim into my code I receive no breakage of
    >any kind upon that line. I have read the eval() statement all over
    >php.net, phpbuilder, phpfreaks and zend and I can't see what I'm doing
    >wrong. Could someone clarify it for me?[/color]

    Well - first I'd wonder if you really need the dangerous eval() at all. What
    about call_user_func?

    call_user_func( array(&$field, $val), $max, $errDisplayArra y[$key]);


    --
    Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
    Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

    Comment

    • Janwillem Borleffs

      #3
      Re: Help with eval()


      "Phil Powell" <soazine@erols. com> wrote in message
      news:1cdca2a7.0 310091333.1fca0 527@posting.goo gle.com...[color=blue]
      > I have this line in my code:
      >
      > eval('$field->' . $val . '(' . $max . ',' . $errDisplayArra y[$key] .
      > ');');
      >[/color]

      Actually, you don't have to use eval() in this case. See the example below:

      class MyClass {
      function echoInput($inpu t) {
      echo $input;
      }
      }

      $myclass = new MyClass;
      $method = "echoInput" ;
      $myclass->$method('Hel lo World');


      JW



      Comment

      Working...