note 85890 modified in function.create-function by felipe
| From: | felipe@php.net | Date: | Tue, 23 Sep 2008 23:23:35 +0000 |
| Subject: | note 85890 modified in function.create-function by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-144652@lists.php.net to get a copy of this message | ||
Workaround is as follows:
<?php
class Foo {
function setHandler($handlerName, $handlerCode) {
$this->handlerFunctions[$handlerName] = create_function('&$self, $dataArr',
$handlerCode);
// NOTE: $this not allowed here, using $self as replacement
}
function executeHandler($handlerName, $dataArr) {
if($this->handlerFunctions[$handlerName]) {
$this->handlerFunctions[$handlerName](&$this, $dataArr);
}
}
function getConstantInfo($what) {
return($what);
}
}
$bla = new Foo;
$bla->setHandler("load",
'echo($self->getConstantInfo("name")."\n");');
$bla->executeHandler("load", array("foo" => "bar"));
?>
--was--
Workaround is as follows:
class Foo {
function setHandler($handlerName, $handlerCode) {
$this->handlerFunctions[$handlerName] = create_function('&$self, $dataArr',
$handlerCode);
// NOTE: $this not allowed here, using $self as replacement
}
function executeHandler($handlerName, $dataArr) {
if($this->handlerFunctions[$handlerName]) {
$this->handlerFunctions[$handlerName](&$this, $dataArr);
}
}
function getConstantInfo($what) {
return($what);
}
}
$bla = new Foo;
$bla->setHandler("load",
'echo($self->getConstantInfo("name")."\n");');
$bla->executeHandler("load", array("foo" => "bar"));
http://php.net/manual/en/function.create-function.php