I'm getting an error using traits and namespaces, beacuse the trait can not be found.
index.php:
require_once 'src/App.php';
use App\main;
$App = new App();
src/App.php
namespace App\main;
require_once __DIR__ . DIRECTORY_SEPARATOR . 'DataBase.php';
/**
* code
*/
src/DataBase.php
namespace App\DataBase;
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Singleton.php';
class DataBase {
use Singleton; // or use App\Singleton
/**
* code
*/
}
src/Singleton.php
namespace App\Singleton.php;
trait Singleton {
/**
* code
*/
}
But, when I run that from Index.php, I'm getting this error:
Fatal error: Trait 'App\DataBase\Singleton' not found in (...)
How can I fix it?
EDIT
Php automatically set the class name in the namespace, for example:
Bar.php
namespace App;
class Bar {
/**
* code
*/
}
The, when you call this package, you can use App\Bar, this means that the classes is setted by default.