Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How do I load a vendor class in a task
  • I guess this is more of a general namespacing / composer issue but basically I have the AWS php sdk installed via composer. I'm trying to write a task that will list all the files in a bucket but I keep getting a class not found, this file is found in my tasks file


    use Aws\S3\S3Client;

    namespace Fuel\Tasks;

    class S3
    {

          public static function run()
          {
                
          }

          public static function list_photos()
          {
                $client = S3Client::factory(\Config::get('aws_config'));

                try {
                      $result = $client->listBuckets();

                      foreach ($result['Buckets'] as $bucket) {
                            // Each Bucket value will contain a Name and CreationDate
                            echo "{$bucket['Name']} - {$bucket['CreationDate']}\n";
                      }
                }
               
                catch (\Exception $e) {
                      \Log::debug($e->getMessage());
                }
          }

    }

    and ill get the error 

    Class 'Fuel\Tasks\S3Client' not found

    how do i tell it to load the class from the vendor directory?
  • HarroHarro
    Accepted Answer
    Assuming your use statement is correct, I think the problem is that "namespace" must be the first executable statement in a PHP file, and "use" most come after that?
  • One of the reasons I rarely use "use" statements these days. Just creates no way of seeing what is happening or what is being called from where (guessing it also removes further class conflicts).

    It's not much harder to call \Aws\S3\S3Client\S3Client::factory() (particularly in this case where there is only 1 reference) and this way it reads cleanly when you revisit your code later on.
  • Apart from an aid for lazy people, you can also "mis-use" the use statement to alias classes. For example you can change

    use Orm\Model;

    by

    use My\Own\ModelClass as Model;

    and you don't have to change your code.

    But I agree that for more common classnames such as "Model", it is difficult to see in your code where it comes from without the namespace prefix.
  • Thanks for the replies, yeah what ended up working was putting namespace first, and I dont know how I didn't know I can just use the full class path to call the class like @hrabbit suggested. 
  • Yes, you can.

    The reason all core classes are aliased is that it makes it easy to extend them. If you use the core class directly, you bypass any extension and that can have consequences.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion