PrestaShop module classes not found (namespaces)

2019-08-23 11:44发布

This is my PrestaShop module file structure:

-mymodule/
--src/
--mymodule.php
---Presta/
---Webhooks.php
----Controller/
-----MyPrestaController.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?

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

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?

1条回答
叼着烟拽天下
2楼-- · 2019-08-23 12:28

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);
}

or

use src\Presta\Webhooks; // before module class declaration

public function hookActionAuthentication($params) 
{ 
    Webhooks::myStaticWebhooksMethod($params);
}
查看更多
登录 后发表回答