How to use namespace in a Class?

Asked

Viewed 2,567 times

9

I am using the AWS SDK for PHP by Amazon and for that I need the class S3Client.

I was looking at an example upload to Amazon and did so:

require '../aws/aws-autoloader.php';

use Aws\S3\S3Client;

$s3 = S3Client::factory($config);

Now I’d like to include the S3Client in my class of communication with Amazon (ConnectionCloud). The problem is it makes the following mistake:

PHP Fatal error: Class 'S3client' not found

How do I declare class 'S3client' within my class ConnectionCloud?

  • 4

    Until today I’m trying to understand the functioning of these name Paces and autoloader, always give some kind of error when I use, if someone has a good tutorial to indicate I accept

  • you have tried to remove the use and do so: $S3 = Aws S3 S3client::Factory($config);

  • @Kaminary gives: > PHP Fatal error: Class 'Aws S3 S3client' not found

  • Can you instantiate by placing a bar at the beginning of the namespace? Like this: use \Aws\S3\S3Client;

  • Not because within my Class I don’t know how to instantiate the S3Client.

  • What is the content of aws-autoloader.php?

  • @Brunoaugusto http://docs.aws.amazon.com/aws-sdk-php/guide/latest/installation.html#Installing-via-zip

  • Right... and how are you trying to do in your class.

  • I tried to do as in the example above, but it doesn’t work. $s3 = S3Client::factory($config); the rest I put in the general PHP script before calling my class.

  • And the autoload file is being included before of your class being instantiated?

  • Sim Bruno Augusto

  • Strange because I copied the files here and tested including the autoload file and when doing a var_dump() of this Factory The data came out right. There are still two possibilities: 1) The autoload file is not being loaded; 2) Although unlikely, its PHP is older than version 5.3, which was when the magic constant DIR used by the autoloader was introduced.

  • I am working with the latest version of PHP

  • And is the file being successfully included? Otherwise the spl_autpload_register() at the end of it will not run. One more possibility: By chance you use __autoload() instead of spl_autoload_register()?

  • @Jorgeb. if you are using any namespace on the page in question, (namespace Jorgeb;) maybe you need a bar in the front \Aws\S3\S3Client;

  • @Rodrigoborth see if my answer helps.

Show 12 more comments

1 answer

9


Your error is probably in the autoload implementation that is not finding the requested class in the use.

Use the Composer to manage your dependencies and classes when working with PHP namespaces. Since it already includes an autoload that supports community standards, you won’t have major problems working external libraries:

Filing cabinet index.php

<?php

// Autoload do Composer
require 'vendor/autoload.php';

// Instancio uma classe qualquer
$cloud = new MyApp\CloudProvider();

echo $cloud->getProviderClass();

Class CloudProvider.php

<?php namespace MyApp;

// Minha classe está acessando uma biblioteca externa, localizada na pasta vendor
use Aws\S3\S3Client;

class CloudProvider {

    protected $provider;

    public function __construct($config)
    {
        $this->provider = S3Client::factory($config);
    }

    public function getProviderClass()
    {
        return get_class($this->provider);
    }

}

composer.json: composer configuration

{
    "require": {
        "aws/aws-sdk-php": "2.7.*"
    },
    "autoload": {
        "psr-4": { "MyApp\\": "" }
    }
}

Example: https://github.com/gmsantos/namespace

Explanation

To make use of PHP namespaces automatically, without including files via require, we need to implement in PHP a autoload and follow a convention to name our classes and folder structure. In that reply we have a simple autoload implementation.

Note that even in a simple implementation, there is a standard for naming classes. The PHP community adopts some standards with specific rules known as PSR-0 and PSR-4 (more about PSR) to name classes and folders.

In order not to need to implement autoload in all our projects manually, we can use a ready-made implementation. The most used is the Composer, which in addition to being compatible with the above standards, is also a dependency manager.

After installing Composer, just create a file composer.json and within it configure the namespace of your application and the path of your files. It will create a folder vendor in its project, which included the dependencies of its project and a autoload.php ready-to-use.

More information on how to install Composer and use you can find here and also in documentationen.

  • I still think it was not including the file with the class/path mapping (which already included an autoloader) or had some other autoloader, most likely one defined with the __autoload() function, overwriting that one.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.