namespace app\index\model; function custom($bcids, $param){ foreach($bcids as $custom){ $file = env('extend_path').'custom/'.$custom['module'].'.php'; if(file_exists($file)){ $res = \custom\$custom['module']::instace()->run(); } } }
类文件在 custom 目录下,现在这样会报错,如果变量换成这样
$res = \custom\Hello::instance()->run();
则不会报错,大神,求助该怎么写?
![]() | 1 frozenway OP 是 TP5.1 的 |
![]() | 2 Fishdrowned 2021-01-11 18:17:01 +08:00 ![]() $res = "\\custom\\{$custom['module']}"::instance()->run(); |
3 ben1024 2021-01-11 18:22:40 +08:00 ![]() 1.字符拼接 "{$xxx}" 2.exec 执行脚本 |
![]() | 4 jswh 2021-01-11 18:26:38 +08:00 ![]() 动态访问时基于字符串的,你可以简单的理解为编译器会先把字符替换到代码里,再执行代码, \custom\$custom['module'],这里的问题时不符合语法规范,namespace 不能后街变量。 所以你得先搞出个完整的带 namespace 的类的字符串,然后再去调用。结果就是 2 楼。 |
![]() | 5 jswh 2021-01-11 18:28:12 +08:00 ![]() @jswh 动态访问是基于字符串的,你可以简单的理解为编译器会先把字符替换到代码里,再执行代码, \custom\$custom['module'],这里的问题是不符合语法规范,namespace 不能后接变量。 所以你得先搞出个完整的带 namespace 的类名的字符串,然后再去调用。结果就是 2 楼。 ------- 着急下班,一堆错字 |
6 rophie123 2021-01-11 18:29:03 +08:00 via iPhone PHP 牛逼 |
![]() | 7 wjfz 2021-01-11 18:32:30 +08:00 ![]() 2 楼正解。 我之前是这么弄的。 $channelClass = "\\common\\services\\sms\\" . ucfirst($channel); if (!class_exists($channelClass)) { throw new NotFoundExceptions("短信发送类{$channelClass}不存在"); } |
![]() | 8 justseemore 2021-01-11 18:49:27 +08:00 ![]() |