PHP create_function 结果存储为实例变量,并调用 $object->func()?
我正在使用 PHP create_function($args, $code)
函数从数据库动态加载函数定义。
我尝试实现它的方式如下:
我有一个类MyClass
,它有一个实例变量myFunction
。构造函数使用 create_function
调用的结果填充该实例变量。我希望为此类的特定对象(实例化后)动态创建一个函数,可以将其称为 $object->myFunction(arg1, arg2);
所以我的类看起来像:
class MyClass {
public $myFunction = '';
public function __construct() {
$this->myFunction = //return function body from DB call.
}
}
然后,我尝试通过执行类似的操作,从程序中的其他位置在实例化的“MyClass”对象上调用此动态函数...
$object = new MyClass();
$object->myFunction(args..);
但是,我不断收到错误,例如:
MyClass and its behaviors do not have a method or closure named myFunction.
当我运行时var_dump($object->myFunction)
我得到了“lambda_xx”,这是一个好兆头,意味着 create_function
至少可以正常工作。
关于有效与无效案例的有趣更新
事实证明,在我的“其他文件”中,我正在执行以下操作:
$pm = Yii::app()->user->postMatching; //This is a PostMatching object made elsewhere
$c = $pm->findRelated;
foreach ($posts as $post) {
var_dump($c);
$postIds = $c($post, $limit);
//post to related mapping
$specificRelatedPostIds[$post->postId] = $postIds;
}
exit; // exiting for testing
这不起作用,但如果不是拉取对象$pm
来自 Yii::app()->user->postMatching
我只是创建了一个新的:
$pm = new PostMatching();
$c = $pm->findRelated; //the anon function instance variable
$c(); // THIS WORKS NOW!
所以很自然地我 var_dumped $pm
并$c
在“新创建”的情况下和我从 Yii::app()->user->postMatching
获取它的情况下,它们是完全相同的。唯一不同的是匿名函数的名称(如预期)。
有谁知道为什么会出现这种情况?在这两种情况下, $pm
都是带有该实例变量的实例化 PostMatching
对象,我只是无法使用语法来调用它!
刚刚用新发现的“Twists”更新了上面的内容,谢谢大家!
I'm using PHPs create_function($args, $code)
function to dynamically load a function definition from a database.
The way I'm attempting to implement it is as follows:
I have a class MyClass
which has an instance variable myFunction
. The constructor populates that instance variable with the result of a call to create_function
. I'm hoping to dynamically create a function for the specific object (once instantiated) of this class, that can be called as $object->myFunction(arg1, arg2);
So my class looks like:
class MyClass {
public $myFunction = '';
public function __construct() {
$this->myFunction = //return function body from DB call.
}
}
I'm then trying to call this dynamic function from elsewhere in my program on the instantiated "MyClass" object by doing something like...
$object = new MyClass();
$object->myFunction(args..);
However I keep getting errors such as:
MyClass and its behaviors do not have a method or closure named myFunction.
When I run var_dump($object->myFunction)
I get back "lambda_xx", which is a good sign meaning create_function
is at least working.
Interesting Update on Works vs. Doesn't Work cases
It turns out that in my "other file" where I am doing the following:
$pm = Yii::app()->user->postMatching; //This is a PostMatching object made elsewhere
$c = $pm->findRelated;
foreach ($posts as $post) {
var_dump($c);
$postIds = $c($post, $limit);
//post to related mapping
$specificRelatedPostIds[$post->postId] = $postIds;
}
exit; // exiting for testing
This doesn't work, but if instead of pulling the object $pm
from Yii::app()->user->postMatching
I just create a new one:
$pm = new PostMatching();
$c = $pm->findRelated; //the anon function instance variable
$c(); // THIS WORKS NOW!
So naturally I var_dumped $pm
and $c
in both the "newly created" case and the case where I get it from Yii::app()->user->postMatching
, and they are identical. The only thing that is different is the name of the anonymous function (as expected).
Does anyone have any idea why this might be the case? In both cases $pm
IS an instantiated PostMatching
object with that instance variable, I'm just unable to use the syntax to invoke it!
Just updated the above with newly discovered "Twists", thanks guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许这些方面的东西可能有用:
Maybe something along these lines can be useful:
这是由于 PHP 存在与解析相关的问题。此版本应该可以工作:
查看实际操作。
That's due to parsing-related troubles that PHP has. This version should work:
See it in action.
您可以像这样调用该方法:
You can call the method like this: