TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   eval - seperate instace. (http://www.talkphp.com/advanced-php-programming/1844-eval-seperate-instace.html)

TlcAndres 01-02-2008 04:08 PM

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;
            }
        } 


sjaq 01-02-2008 08:02 PM

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().


Salathe 01-02-2008 08:28 PM

Quote:

Originally Posted by TlcAndres (Post 7512)
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!

TlcAndres 01-02-2008 08:30 PM

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.


All times are GMT. The time now is 08:31 PM.

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