Php 在课外使用$this
我有一个类,我正在为我的记录扩展几乎每个数据库表。到目前为止,一切正常,但我需要对调用的函数使用Php 在课外使用$this,php,oop,Php,Oop,我有一个类,我正在为我的记录扩展几乎每个数据库表。到目前为止,一切正常,但我需要对调用的函数使用$this 这是我的班级,例如: class myClass { public function __call($method, $args) { if (isset($this->$method)) { $func = $this->$method; return call_user_func_array($
$this
这是我的班级,例如:
class myClass
{
public function __call($method, $args)
{
if (isset($this->$method)) {
$func = $this->$method;
return call_user_func_array($func, $args);
}
}
}
这是我要测试的功能:
$testFunction = function($variable1) {
$this->_temp_instance = clone $this;
}
我分配给我的班级:
$class_instance = new myClass();
$class_instance->new_assigned_function = $testFunction;
$class_instance->new_assigned_function();
我得到一个错误:
Fatal error: Uncaught Error: Using $this when not in object context in...
我可以在不编辑可扩展类或继承类的情况下完成它吗
我有太多的课程:/
谢谢。你想要实现的目标(以及你为什么想要实现这一目标)对我来说似乎很难理解,你所追求的几乎可以用其他更干净的方式解决,但是-回答你的问题 您可以
$instance = new myClass;
$boundFunction = $testFunction->bindTo($instance, $instance);
$instance->func = $boundFunction;
$instance->func();
此
在测试功能中的用途是什么?如果您想在类之间共享方法,可以使用traits,请查看它们。@Ali,这只是为了说明我目前的主要原因。。。我的目的是获取类的属性、方法等。如果您想要对象和类的元数据,为什么不使用内置的反射工具呢?在我看来,你似乎对OOP缺乏共识,只是试图重新发明轮子,它可以承载4节车厢,而常识则相反——你需要4个轮子来承载1节车厢。