TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   eval() issue (http://www.talkphp.com/general/5979-eval-issue.html)

exangelus 09-05-2011 09:48 AM

eval() issue
 
Hey all,

I'm new here and like to join the community to ask & answer!
From the netherlands as a airline pilot student and part-time php programmer :)

At my current project I got a situation in which I call a method from a child class and the parameter's are also defined in a config file. So using the config file info I build up the correct argument order, the variables are defined before that. Now I wanna use eval() to call my method but I get the following error:

Quote:

A PHP Error was encountered

Severity: 4096

Message: Object of class Rapport could not be converted to string

Filename: controllers/rapport.php

Line Number: 62
The code:

PHP Code:

//argumenten ophalen
                
$arguments $rapportages[$rapportage]['arguments'];

                
//argumenten definen
                
foreach($arguments as $arg) {
                    $
$arg false;
                }
                
                
//daadwerkelijk bekende argument definen
                
$$sorteer_methode $sorteer_optie;
                
                
//argumentvolgorde maken voor eval()
                
$arg_order '';
                foreach(
$arguments as $arg) {
                    
$arg_order .= '$' $arg ', ';
                }
                
$arg_order substr($arg_order0, -2);
                
                
//functie aanroepen
                
eval("$this->$method($arg_order)"); 

$sorteer_optie is the argument I know and the others are already set false, this all seems ok but the eval() call doesn't work.
Think I need to escape it in some way but just putting a backward slash in front of $this won't work..

Thanks in advance!

exangelus 09-05-2011 10:01 AM

Already solved it now by changing the call into:
PHP Code:

$this->$method(eval("$arg_order")); 


maeltar 09-05-2011 10:24 AM

Welcome to the forums, they are a little quite at the moment, but am sure they will start getting busy once again.

exangelus 09-05-2011 12:55 PM

Thanks for the welcome :)

Thought I'd fixed it but nope.. it just seems the whole arg_order var as the first argument of the function.. quickfixed it this way but it's really ugly and there has to be a way to automate this:

PHP Code:

if(count($args) == 0) {
                    
$this->$method();
                } elseif(
count($args) == 1) {
                    
$this->$method($args[0]);
                } elseif(
count($args) == 2) {
                    
$this->$method($args[0], $args[1]);
                } elseif(
count($args) == 3) {
                    
$this->$method($args[0], $args[1], $args[2]);
                } 

Ofcourse I could simply send the array to the method but I also call the method in other ways where I strongly dislike the use of an array and I also dislike the option to alternatively accept both the normal way and the array way IF there are better options.. :-P

tony 09-07-2011 01:28 PM

I was going to suggest to accept in array form or multiple args, but you just said you don't like that. Welcome aboard!

wGEric 09-09-2011 10:13 PM

Check out call_user_func_array.

PHP Code:

call_user_func_array(array($this$method), $args); 


exangelus 09-11-2011 01:37 PM

That's a nice solution! Thanks guys :)

Parkinson4 11-28-2011 11:15 AM

I would be cautious in calling eval() pure evil. Dynamic evaluation is a powerful tool and can sometimes be a life saver. With eval() one can work around shortcommings of PHP (see below).
The main problems with eval() are:
• Potential unsafe input. Passing an untrusted parameter is a way to fail. It is often not a trivial task to make sure that a parameter (or part of it) is fully trusted.
• Trickyness. Using eval() makes code clever, therefore more difficult to follow. To quote Brian Kernighan "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it"
The main problem with actual use of eval() is only one:
• Inexperienced developers who use it without enough consideration.
As mentioned before eval() can help you do things that are impossible in pure PHP. My favourite trick involving dynamic evaluation enables static calls on variable classes. Since $foo::bar() is illegal in PHP, below solution works around that limitation.
$className = 'Foo';
eval('$result = ' . $className . '::bar()');
echo $result;
As a rule of thumb I tend to follow this:
1. Sometimes eval is the only/the right solution.
2. For most cases one should try something else.
3. If unsure, goto 2.
4. Else, be very, very careful.

wGEric 11-29-2011 06:10 PM

Quote:

Originally Posted by Parkinson4 (Post 32464)
My favourite trick involving dynamic evaluation enables static calls on variable classes. Since $foo::bar() is illegal in PHP, below solution works around that limitation.

PHP 5.3 lets you do that :-)

I tend to find that those that use eval don't know the language well enough to do it any other way. There are very few cases to use eval. Your example can be done with the following code:

PHP Code:

<?php

class Foo {
    public static function 
bar() {
        return 
'bar';
    }
}

$foo 'Foo';

$result1 call_user_func_array($foo '::bar', array());
echo 
$result1;

$result2 call_user_func_array(array($foo'bar'), array());
echo 
$result2;

// $result3 = $foo::bar(); works in PHP 5.3
// echo $result3;



All times are GMT. The time now is 05:59 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0