Re: [PHP4BETA] eval'ing a method
| From: | Faisal Nasim | Date: | Fri, 05 May 2000 17:57:35 +0000 |
| Subject: | Re: [PHP4BETA] eval'ing a method | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-14543@lists.php.net to get a copy of this message | ||
| <?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!
This will work: (but it isn't what you wanted to do)
<?php
$m = 'function somefunc() { return 2; }';
eval($m);
class blah {
var $somevar = 1;
function somemethod()
{
$this->somevar = somefunc();
echo "this->somevar=$this->somevar<br>\n";
}
}
$x = new blah;
$x->somemethod();
echo "x->somevar=$x->somevar<br>\n";
?>
Enjoy!
Faisal