找不到PrestaShop模块类(命名空间) [英] PrestaShop module classes not found (namespaces)
问题描述
这是我的PrestaShop模块文件结构:
This is my PrestaShop module file structure:
-mymodule/
--src/
--mymodule.php
---Presta/
---Webhooks.php
----Controller/
-----MyPrestaController.php
mymodule.php找不到Webhooks.php类,我已经尝试在mymodule.php中使用它,但是它仍然提供错误:
mymodule.php cannot find Webhooks.php class, I've tried use in mymodule.php, but still it provides errors:
ClassNotFoundException in mymodule.php line 55:
Attempted to load class "Webhooks" from namespace "src\Presta".
Did you forget a "use" statement for another namespace?
当我尝试在mymodule.php中使用autoload/include/require时,它将引发致命错误,因为autoload初始化了不应在mymodule.php中初始化的东西(来自我的模块供应商). GuzzleClient在浏览网站时变得疯狂:
When I try to use autoload/include/require in mymodule.php it throws fatal errors, because autoload initialize stuff(from my module vendor) that shouldn't be initialized in mymodule.php. GuzzleClient gets crazy while browsing website:
Catchable Fatal Error: Argument 3 passed to
GuzzleHttp\Client::request() must be of the type array, string given,
called in /usr/local/ampps/www/presta/modules/mymodule/vendor/guzzlehttp/guzzle/src/Client.php on line 89 and defined
我不想将所有的钩子逻辑放在mymodule.php中,而我还有其他需要在webhook方法中实现的类.有什么办法可以在主模块文件(mymodule.php)中使用其他类?我想念什么吗?
I don't want to put all hook logic in mymodule.php and I have other classes that I need to implement in webhook methods. Is there any way to use other classes in main module file(mymodule.php)? Am I missing something?
推荐答案
您需要使用完整路径调用类,或在模块文件顶部声明use
.我不知道确切的名称空间Webhooks
在什么下面,但是像这样:
You need to call your class with full path or declare a use
on top of your module file. I don't know exactly what namespace Webhooks
is under but something like this:
public function hookActionAuthentication($params)
{
\src\Presta\Webhooks::myStaticWebhooksMethod($params);
}
或
use src\Presta\Webhooks; // before module class declaration
public function hookActionAuthentication($params)
{
Webhooks::myStaticWebhooksMethod($params);
}
这篇关于找不到PrestaShop模块类(命名空间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!