【发布时间】:2019-08-30 02:50:09
【问题描述】:
是否有任何聪明的解决方案可以从父类__construct 中获取被调用的方法?
示例:
call_user_func_array([$childrenController, $indexMethod], array());
我想知道 $indexMethod 被调用了,但我尝试在 $parentController __construct 中获取此信息。
我知道我可以用
获得类名get_called_class()
或者通过ReflectionClass,但是被调用的方法有什么解决办法吗?
已经试过了
debug_backtrace()
但我只能获得关于另一个方向的任何信息。
编辑:
有人打电话给我的网站: https://example.com/index/firstaction
然后路由器正在搜索控制器:indexcontroller 和方法:firstaction。
索引控制器扩展了 ParentController 类。
现在我想在 indexcontroller 的 parent(ParentController) 中获取被调用方法的名称(firstaction)
class ParentController {
public function __construct()
{
}
【问题讨论】:
-
请提供完整的样本以及您何时/何地需要哪些信息,目前还不清楚。
-
I want to know that $indexMethod got called- 为什么这对parent很重要。您可以设置父母和孩子都拥有indexMethod(){ $this->called = 1;}的属性,然后检查if($this->called)等。 -
我想通过传递文件夹名(称为类)和文件名(称为方法)动态地告诉 smarty 模板的目录。所以我认为最好的解决方案是在任何控制器继承的 ParentController 中执行此操作。
-
ParentController::construct在对象的任何其他方法之前被调用,因此您无法预测从那里会发生什么。除非我误解了你的意思。
标签: php methods get parent construct