php - 在 php 类中的对象中调用方法
问题描述
我需要调用一个对象内部、类内部的函数。当然,对于“On The Fly”类方法,我可以使用 __call 和 __set 魔法来调用它,但在这种情况下不能。下面是这种情况的示例。
class mainclass
{
public $v1 = "Hello";
public $fn = null;
function __construct( )
{
$this->fn = (object) [ "fn1" => null,
"fn2" => null,
];
}
public function __call( $name, array $args )
{
return call_user_func_array( $this->$name, $args );
}
public function fn3()
{
echo "This of course works! <br />";
}
}
$main = new mainclass();
$main->fn4 = function()
{
echo "Even this works! <br />";
};
$main->fn->fn1 = function()
{
echo $this->v1 . " World :)";
};
$main->fn3(); // This of course works!
$main->fn4(); // Even this works!
$main->fn->fn1(); //Call to undefined method stdClass::fn1()
有可能以这种方式调用函数“f1”: $main->fn->fn1() ?如果没有,有没有大刀阔斧的建议?
不幸的是,这不是 JavaScript 并且不喜欢处理此类的方式,但我必须尝试一下
解决方案
对于这种情况,我唯一且简单的解决方法是更改匿名类中的对象。在此过程中,您必须使用类似的变量名称“$_this”将主类的范围存储在内部匿名类上。
class mainclass
{
public $v1 = "Hello";
public $fn = null;
function __construct( )
{
$this->fn = new class( $this)
{
public $_this = null;
public function __construct( $mainscope )
{
$this->_this = &$mainscope;
}
public function __call( $method, array $args )
{
if ( isset( $this->{ $method } ) )
{
return call_user_func_array( $this->$method, $args );
}
elseif ( isset( $this->_this->{ $name } ) )
{
return call_user_func_array( $this->_this->{ $name }, $args);
}
}
public function __set( $name, $value )
{
$this->{ $name } = is_callable( $value ) ? $value->bindTo( $this, $this ) : $value;
}
};
}
public function __call( $method, array $args )
{
return call_user_func_array( $this->{ $method }, $args );
}
public function __set( $name, $value )
{
$this->{ $name } = is_callable( $value ) ? $value->bindTo( $this, $this ) : $value;
}
public function fn3()
{
echo "This of course works! <br />";
}
}
$main = new mainclass();
$main->fn4 = function()
{
echo "Even this works! <br />";
};
$main->fn->fn1 = function()
{
echo $this->_this->v1 . " World :)";
};
$main->fn3(); // This of course works!
$main->fn4(); // Even this works!
$main->fn->fn1(); //Hello World :)
事实证明,它不是很丑陋,也可以管理。无论如何,这是目前唯一的选择。
推荐阅读
- sql-server - 在 kafka 中使用 kafka-connect-jdbc 在 MS SQL 表中摄取嵌套 Avro 时出错
- python - Python 3 中更苗条的替代方案是什么?
- python - qml 文件不能调用函数到 pyd 文件
- ios - weex的高度
成分不准确? - performance - Oracle - Regexp_substr 和 Instr 之间的性能
- python - 在 Windows 上安装 anaconda / miniconda 的默认位置是什么?
- c# - 多线程 Web API 应用程序
- r - 使用 uniroot.all 查找包含数组的函数的根
- scala - 如何在 HDFS 中编写黄瓜 json 报告
- docker - 如何让 Eureka 客户端使用主机 IP 而不是 Docker 容器的 IP?