Quote:
Originally Posted by TlcAndres
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:
In the context of your function, the appropriate variables to use would be:
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!