0

I recently discovered the PHP plex-api project, which provides a PHP-friendly means of working with the PLEX overall API.

I've had success as far as the general top-level API, but have been unable to figure out how to use the Filter (and other) classes that are intended to expand the capability. In short, I have the following code:

require_once('vendor/autoload.php') ;
use jc21\PlexApi ;
require_once('vendor/jc21/plex-api/src/jc21/Util/Filter.php') ;
$client = new PlexApi($creds['hostname']);
$client->setAuth($creds['userId'], $creds['password'] ) ;

... and I can successfully use the $client properties and methods. But when I try to execute this line:

filter1 = new Filter('title', $titleToFind) ;

I get a Class not found error. This confuses me since the (required) Util/Filter.php file starts with this code (Blank lines and comments removed)

namespace jc21\Util;
use Exception;
class Filter
{
    private string $field;

And so on.

I suspect it's the "use" keyword that's giving me fits (I consider myself pretty good with PHP but this one throws me for a loop - it's a new concept for me).

I didn't find a community reference in the github page for this (link above) so I have come here for help.

Any pointers?

Thanks!

Dennis
  • 1,071
  • 2
  • 17
  • 38
  • You need to add a `use namespace\classname ;` for each class you want to import. Your `use jc21\PlexApi ;` only imports that specific class to your current namespace. If you want to do the same with the `Filter` class, you need to add `use jc21\Util\Filter;` as well. You can read more about `use` in the manual: https://www.php.net/manual/en/language.namespaces.importing.php – M. Eriksson Aug 27 '22 at 16:23
  • Btw, I can see that this library is in a `vendor`-folder. Are you possibly using composer? If so, you should only include the `vendor/autoload.php` file right in the beginning of your application. Then you won't need `include` any files from the libraries installed using composer since composers autoloader will handle that. – M. Eriksson Aug 27 '22 at 16:28
  • Thanks @M.Eriksson - Yes, Composer. I appreciate the responses and advice. I was working with the very limited example at https://github.com/jc21/plex-api/blob/master/docs/Documentation.md. I'll be home again tomorrow; can't wait to try your suggestions. – Dennis Aug 28 '22 at 17:08
  • 1
    @M.Eriksson - thanks again, you did get me past the error. I did already have require_once('vendor/autoload.php') ; at the top of the app, but added the other in hopes to avoid the problem you see. You've set me straight now. Wonderful! – Dennis Aug 29 '22 at 13:55

0 Answers0