Re: [PHP4BETA] eval'ing a method
| From: | Faisal Nasim | Date: | Fri, 05 May 2000 18:19:01 +0000 |
| Subject: | Re: [PHP4BETA] eval'ing a method | ||
| References: | 1 2 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-14547@lists.php.net to get a copy of this message | ||
Addon to my message:
You can eval() the whole class to get what you want to do!
$m = 'function somefunc() { $this->somevar = 2; }';
eval ( '
class blah {
var $somevar = 1;
'
. $m .
'
function somemethod()
{
$this->somefunc();
echo "this->somevar=$this->somevar<br>\n";
}
}
'
);
$x = new blah;
$x->somemethod();
echo "x->somevar=$x->somevar<br>\n";
Cool eh?
| | <?php
| | class blah {
| | var $somevar = 1;
| |
| | function somemethod() {
| | $m = "function somefunc() { $this->somevar = 2; }";
|
| $this will be resolved, better wrap in single quotes.
|
| | eval($m);
| | $this->somefunc();
| | echo "this->somevar=$this->somevar<br>\n";
| | }
| | }
| | $x = new blah;
| | $x->somemethod();
| | echo "x->somevar=$x->somevar<br>\n";
| | ?>
| |
|
| This won't work. A function within a function doesn't
| go into a global scope!
|
| function blah ()
| {
| print "blah";
|
| function etc()
| {
| print "etc";
| }
| }
|
| blah() will work but etc() doesn't exist in global scope!
| You can call etc() from blah() after the declaration of
| the function!
|
|
<cut>