Re: [PHP4BETA] zend engine does not provide full variable reference for variable functions
| From: | Faisal Nasim | Date: | Fri, 12 May 2000 10:50:19 +0000 |
| Subject: | Re: [PHP4BETA] zend engine does not provide full variable reference for variable functions | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-14895@lists.php.net to get a copy of this message | ||
| I want to access this method $this->db->query(), that works of course.
| 1.) slightly changed (but for my function useless) it still works
|
| $a='db';
| $b='query';
|
| $this->$a->$b() // this works too
This is a loose OOP model, to directly convert string context
into the class member. But its handy for interpreted languages!
(I think something like this should be allowed only through eval()
only though)
|
| 2.) This does not work
|
| $str='db->query';
| $this->$str();
The the above example #1:
$this -> $a -> $b ()
$a is converted to 'db' and which is looked up in the current
scope (function, global or class) for its type, it finds another
class the next "->" is another token and the class pointer
is now actually $this -> db, and query is called as function
to the last class pointer it has ($this -> db -> query ()).
But in $str = 'db->query', its treated as a literal string and
'db->query' is considered as a single token (string, and not
an object/function pointer) instead of two as in the first case.
It looks up 'db->query' in the scope, but doesn't find any match.
Infact, it might give an error from the routine which checks the
variable consistency. You have call eval() for this! People who
have dealt with 'yacc/lex' will know this very well! :)
Try this:
class Y { function play () { print "Play what?\n"; } }
class X
{
var $y;
function X () { $this -> y = new Y; }
function play ()
{ $a = 'y'; $b = 'play'; $this -> $a -> $b (); }
function play2 ()
{ $a = 'y->play'; eval ( "\$this->$a();" ); }
function play3 ()
{ $a = 'this->y->play()'; eval ( "\$$a;" ); }
}
$x = new X;
$x -> play ();
$x -> play2 ();
$x -> play3 ();
Enjoy!
Faisal