0

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?

maciey_b
  • 147
  • 1
  • 11
  • Which version of prestashop and how are you calling Webhooks class? – TheDrot May 08 '18 at 15:43
  • 1.7.3 and example Webhook class called in: public function hookActionAuthentication($params) { Webhooks::myStaticWebhooksMethod($params); } – maciey_b May 09 '18 at 06:12

1 Answers1

0

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);
}
TheDrot
  • 4,246
  • 1
  • 16
  • 26
  • Finally I used require_once in mymodule main file, presta doesn't like custom namespaces and there is problem with use. But the main reason I was getting errors was conflict between mymodule GuzzleHttp6 and prestaShop GuzzleHttp5 lib, had to remove GuzzleHttp from my module and use the core presta one. – maciey_b May 11 '18 at 07:01
  • @maciey_b In PrestaShop files such as module or controller classes I call my custom classes with full namespace path and I use composer's PSR-4 autoloader. And yea those library conflicts is what's called a dependency hell. – TheDrot May 11 '18 at 11:06