Frank Vèssia Posted July 31, 2022 Share Posted July 31, 2022 Hello, I'm trying to get to work a getstream.io library inside a module. Since I'd like to avoid using composer I downloaded the library from php-download.com which makes it possible just to include the autoloader.php file and using the library directly. I tested it inside a template and it works well but I can't inside a module. This is what I wrote so far inside template: <?php require_once('./vendor/autoload.php'); $client = new GetStream\Stream\Client($key, $secret); Inside module I get error: class GetStream\Stream\Client not found <?php namespace ProcessWire; class ProcessSocial extends WireData implements Module, ConfigurableModule { ... public function init() { $file = __DIR__ . '/vendor/autoload.php'; if (file_exists($file)) { require_once $file; } } protected function testFeed($user){ $client = new GetStream\Stream\Client($api, $key); $feed = $client->feed('User', $user); return $feed->getActivities(); } } Link to comment Share on other sites More sharing options...
Frank Vèssia Posted July 31, 2022 Author Share Posted July 31, 2022 So I got it...just removing "namespace Processwire" on top of the module 1 Link to comment Share on other sites More sharing options...
pwired Posted July 31, 2022 Share Posted July 31, 2022 Quote Since I'd like to avoid using composer I downloaded the library from php-download.com which make it possible just to include the autoloader.php file and using the library directly. So I got it ... just removing "namespace Processwire" on top of the module ... Thought that I was the only one trying to avoid composer. Thanks for posting this working solution (added to my snippets) 2 Link to comment Share on other sites More sharing options...
LMD Posted July 31, 2022 Share Posted July 31, 2022 To use a namespaced (or non-namespaced) class inside another namespaced file, you need to add a slash ('\') before the included classes' namespace. Otherwise, it is looking for the class relative to the file's namespace. While it works -- and in this instance doesn't appear to cause issues -- removing a file's namespace negates the reason for using namespaces in the first place. Here is an ammended version of your code: <?php namespace ProcessWire; class ProcessSocial extends WireData implements Module, ConfigurableModule { public function init() { $file = __DIR__ . '/vendor/autoload.php'; if (file_exists($file)) { require_once $file; } } protected function testFeed($user){ // Note the '\' before 'GetStream' $client = new \GetStream\Stream\Client($api, $key); $feed = $client->feed('User', $user); return $feed->getActivities(); } } If you find you are calling a class a lot, then you can use a 'use' statement at the top of the file, like this example: <?php namespace ProcessWire; use \GetStream\Stream\Client; // Namespace path to the classname (inc. the classname too) class ProcessSocial extends WireData implements Module, ConfigurableModule { public function init() { $file = __DIR__ . '/vendor/autoload.php'; if (file_exists($file)) { require_once $file; } } protected function testFeed($user){ // Now you don't need to add the namespace at all here $client = new Client($api, $key); $feed = $client->feed('User', $user); return $feed->getActivities(); } } Hope that helps. 2 Link to comment Share on other sites More sharing options...
Frank Vèssia Posted July 31, 2022 Author Share Posted July 31, 2022 49 minutes ago, LMD said: To use a namespaced (or non-namespaced) class inside another namespaced file, you need to add a slash ('\') before the included classes' namespace. Otherwise, it is looking for the class relative to the file's namespace. While it works -- and in this instance doesn't appear to cause issues -- removing a file's namespace negates the reason for using namespaces in the first place. Here is an ammended version of your code: <?php namespace ProcessWire; class ProcessSocial extends WireData implements Module, ConfigurableModule { public function init() { $file = __DIR__ . '/vendor/autoload.php'; if (file_exists($file)) { require_once $file; } } protected function testFeed($user){ // Note the '\' before 'GetStream' $client = new \GetStream\Stream\Client($api, $key); $feed = $client->feed('User', $user); return $feed->getActivities(); } } If you find you are calling a class a lot, then you can use a 'use' statement at the top of the file, like this example: <?php namespace ProcessWire; use \GetStream\Stream\Client; // Namespace path to the classname (inc. the classname too) class ProcessSocial extends WireData implements Module, ConfigurableModule { public function init() { $file = __DIR__ . '/vendor/autoload.php'; if (file_exists($file)) { require_once $file; } } protected function testFeed($user){ // Now you don't need to add the namespace at all here $client = new Client($api, $key); $feed = $client->feed('User', $user); return $feed->getActivities(); } } Hope that helps. Much appreciated, thanks Link to comment Share on other sites More sharing options...
bernhard Posted July 31, 2022 Share Posted July 31, 2022 2 hours ago, LMD said: If you find you are calling a class a lot, then you can use a 'use' statement at the top of the file, like this example: Also note that modern IDEs do that automatically for you once you start writing "new Client" in your code ? 2 Link to comment Share on other sites More sharing options...
LMD Posted August 1, 2022 Share Posted August 1, 2022 16 hours ago, bernhard said: Also note that modern IDEs do that automatically for you once you start writing "new Client" in your code ? That, and many other reasons, is why I use one ? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now