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 01-02-2008, 04:08 PM   #1 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default eval - seperate instace.

Does eval() create a seperate parsing instance? to clear up the question are any variables function and classes not available within the eval() function? the reason I ask is because out of curiosity I created this...

PHP Code:
public function __call($funcName,$args)
        {
            
$classArr = array(
                    
'm_' => 'member',
                    
't_' => 'template',
                    
'f_' => 'file',
                    
'u_' => 'upload',
                    
'd_' => 'mysql'
                
);
            
$class substr($funcName,0,2);
            
$func str_replace($class,'',$funcName);
            
$func preg_replace('$\\((.*?)\\)$','',$func);
            
$order $classArr[$class];
            if(!empty(
$order))
            {
                for(
$i=0;$i func_num_args();++$i)
                {
                    
$argList .= func_get_arg($i) . ',';
                }
                
                
$c substr($argList,strlen($argList) -1,1);
                if(
$c == ',')
                {
                    
$argList substr($argList,0,strlen($argList) -1);
                }
                
$string sprintf('$this->%s->%s(%s);',$order,$func,'o');
                echo 
$string;
                
ob_start();
                eval(
$string);
                
$ret ob_get_clean();
                return 
$ret;
            }
        } 
TlcAndres is offline  
Reply With Quote
Old 01-02-2008, 08:02 PM   #2 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

You don't need to use eval for the thing you are trying to achieve, you could just replace:
PHP Code:
$string sprintf('$this->%s->%s(%s);',$order,$func,'o');
[...
snip...]
eval(
$string); 
with:
PHP Code:
$string $this->$order->$func('o'); 
or:
PHP Code:
$string call_user_func_array(array($this->$order$func), array('o'); 
But to answer your question, eval() uses the same scope as a normal block of code..
Quote:
Originally Posted by phpdig.net
eval() behaves as if the string being evaluated was a normal block of code in the same scope as the call to eval().
sjaq is offline  
Reply With Quote
Old 01-02-2008, 08:28 PM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by TlcAndres View Post
Does eval() create a seperate parsing instance? to clear up the question are any variables function and classes not available within the eval() function?
No, eval simply evaluates the code string as if it were hard-coded at that point in the script -- the variables, functions, etc. are all available in the current scope just like regular code.

Your function seems a little odd. What would the advantage be of calling $object->t_display('blah') over $object->template->display('blah')? The former seems (to me) counter-productive since you're in essence breaking down the owner/member object relationship... in broad terms you're calling a method (function) "belonging" to $object when that method actually doesn't (shouldn't) belong to it; display belongs to template, not to the $object directly. But that's an aside, and I'm sure you have perfectly valid reasons for taking this particular approach.

There are a number of things that I don't particularly feel are good approaches in your function, but since you didn't ask for comments on the actual code, I'll leave those out of this post. I would like to say however that you can use call_user_func_array as an alternative to eval. Say for example that the string which you would have evaled turned out to be: $this->template->display('blah', 'moo'); then you can use call_user_func_array in the following manner:
PHP Code:
call_user_func_array(array($this->template, 'display'), array('blah', 'moo'));

In the context of your function, the appropriate variables to use would be:
PHP Code:
call_user_func_array(array($this->$order, $func), $args);

A revised alternative (not that you asked for one) might be something along the lines of:
PHP Code:
public function __call($szMethod, $aArgs)
{
    // Making $aMembers static would be awesome
    $aMembers = array('m_' => 'member',
                      't_' => 'template',
                      'f_' => 'file',
                      'u_' => 'upload',
                      'd_' => 'mysql');
                      
    $szStub   = substr($szMethod, 0, 2);
    $szMethod = substr($szMethod, 2);
   
    // Error handling would be nice here. :)

    // Call the method on the appropriate member object
    return call_user_func_array(array($this->$aMembers[$szStub], $szMethod), $aArgs);
}

Edit: sjaq beat me to it... guess that serves me right for drafting a post then wandering off for a cup of tea before submitting it!
Salathe is offline  
Reply With Quote
Old 01-02-2008, 08:30 PM   #4 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default

Actually if you have any comments and such I'd like to see in order to improve them and the reason why I chose that was actually out of simple curiosity something like a proof of concept.
TlcAndres 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


All times are GMT. The time now is 01:04 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