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.
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
– RodrigoBorth
you have tried to remove the use and do so: $S3 = Aws S3 S3client::Factory($config);
– jlHertel
@Kaminary gives: > PHP Fatal error: Class 'Aws S3 S3client' not found
– Jorge B.
Can you instantiate by placing a bar at the beginning of the namespace? Like this:
use \Aws\S3\S3Client;
– Rodrigo Rigotti
Not because within my Class I don’t know how to instantiate the
S3Client
.– Jorge B.
What is the content of aws-autoloader.php?
– Bruno Augusto
@Brunoaugusto http://docs.aws.amazon.com/aws-sdk-php/guide/latest/installation.html#Installing-via-zip
– Jorge B.
Right... and how are you trying to do in your class.
– Bruno Augusto
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.– Jorge B.
And the autoload file is being included before of your class being instantiated?
– Bruno Augusto
Sim Bruno Augusto
– Jorge B.
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.
– Bruno Augusto
I am working with the latest version of PHP
– Jorge B.
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()?
– Bruno Augusto
Let’s go continue this discussão in chat.
– Jorge B.
@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;
– Papa Charlie
@Rodrigoborth see if my answer helps.
– gmsantos