NYCPHP Meetup

NYPHP.org

[nycphp-talk] I need help with eval() with a class void method call

Daniel Kushner nyphp at websapp.com
Thu Oct 9 17:49:25 EDT 2003


You can try using the call_user_func function here:

<?php
class MyClass {
    function setMaxLength($max, $errorMsg) {
        echo $errorMsg.' ('.$max.')';
    }
}


$myObj = new MyClass();

call_user_func(array($myObj,'setMaxLength'), 40, 'The last name cannot be
more than 40 characters');
?>


--Daniel



> -----Original Message-----
> From: talk-bounces at lists.nyphp.org
> [mailto:talk-bounces at lists.nyphp.org]On Behalf Of soazine at pop.erols.com
> Sent: Thursday, October 09, 2003 5:31 PM
> To: talk at lists.nyphp.org
> Subject: [nycphp-talk] I need help with eval() with a class void method
> call
>
>
> I have this line in my code:
>
> eval('$field->' . $val . '(' . $max . ',' .
> $errDisplayArray[$key] . ');');
>
> where $field is a predefined class instance, $val is a string containing
> the name of an existing method within the class, and
> $errDisplayArray[$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(40,"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
>
> --------------------------------------------------------------------
> mail2web - Check your email from the web at
> http://mail2web.com/ .
>
>
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk
>





More information about the talk mailing list