【发布时间】:2023-02-21 00:23:57
【问题描述】:
当在 php/laravel 中使用 use 关键字时,有没有办法执行方法/脚本?
我有一个班级和一个特质
每当在类中使用时,我都想在特征中执行一个方法(仅一次) 我认为在 trait 的构造函数中这样做是可行的,而且确实如此,直到我遇到一个只有静态方法的类。
例如:
class A {
use B;
public static function hello() {
return "hello";
}
}
trait B {
function i_want_to_execute_on_use_keyword_but_just_once() {
return "Executing important things";
}
}
A::hello(); ---\> B's method should execute before this\`
php/laravel 中有什么东西可以让我执行这种行为吗?
注意:在我的实际场景中,我需要对使用特征的类运行反射。
【问题讨论】: