TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-05-2011, 09:48 AM   #1 (permalink)
The Visitor
 
Join Date: Sep 2011
Location: Eindhoven, Netherlands
Posts: 4
Thanks: 1
exangelus is on a distinguished road
Default 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 is offline  
Reply With Quote
Old 09-05-2011, 10:01 AM   #2 (permalink)
The Visitor
 
Join Date: Sep 2011
Location: Eindhoven, Netherlands
Posts: 4
Thanks: 1
exangelus is on a distinguished road
Default

Already solved it now by changing the call into:
PHP Code:
$this->$method(eval("$arg_order")); 
exangelus is offline  
Reply With Quote
Old 09-05-2011, 10:24 AM   #3 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

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

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 09-05-2011, 12:55 PM   #4 (permalink)
The Visitor
 
Join Date: Sep 2011
Location: Eindhoven, Netherlands
Posts: 4
Thanks: 1
exangelus is on a distinguished road
Default

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..
exangelus is offline  
Reply With Quote
Old 09-07-2011, 01:28 PM   #5 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

I was going to suggest to accept in array form or multiple args, but you just said you don't like that. Welcome aboard!
tony is offline  
Reply With Quote
Old 09-09-2011, 10:13 PM   #6 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

Check out call_user_func_array.

PHP Code:
call_user_func_array(array($this$method), $args); 
__________________
Eric
wGEric is offline  
Reply With Quote
The Following User Says Thank You to wGEric For This Useful Post:
exangelus (09-11-2011)
Old 09-11-2011, 01:37 PM   #7 (permalink)
The Visitor
 
Join Date: Sep 2011
Location: Eindhoven, Netherlands
Posts: 4
Thanks: 1
exangelus is on a distinguished road
Default

That's a nice solution! Thanks guys :)
exangelus is offline  
Reply With Quote
Old 11-28-2011, 11:15 AM   #8 (permalink)
The Visitor
 
Parkinson4's Avatar
 
Join Date: Nov 2011
Posts: 3
Thanks: 0
Parkinson4 is on a distinguished road
Default

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.
Parkinson4 is offline  
Reply With Quote
Old 11-29-2011, 06:10 PM   #9 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

Quote:
Originally Posted by Parkinson4 View Post
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;
__________________
Eric
wGEric is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Str _replace Issue Tim Dobson Absolute Beginners 2 03-05-2011 06:29 PM
Template Class Issue tego10122 Advanced PHP Programming 1 12-28-2010 07:08 PM
Frustrating PNG Issue on IE 6 aristoworks XHTML, HTML, CSS 5 11-10-2008 08:18 AM
eval remake TlcAndres Tips & Tricks 2 05-23-2008 10:41 AM
eval - seperate instace. TlcAndres Advanced PHP Programming 3 01-02-2008 08:30 PM


All times are GMT. The time now is 05:00 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design