There is a structure:

| - index.php | - test | classes |- someclass.php | traits |- sometrait.php 

And the code:

(Index.php - works)

 <?php use \Test\Classes as Classes; use \Test\Traits as Traits; spl_autoload_register(...); echo Classes\Someclass::start(); ?> 

(Someclass.php)

 <?php namespace Classes; // ^ не работает, но если я заменю "Classes" на "Test\Classes", то работает // Почему? Class Someclass { use Traits/Sometrait; // Да, не работает, но если я заменю "traits" на "Test\Traits" , то работает! // Почему? public static function start() {...} } ?> 

The fact is that the classroom namespace works only if you write Test\Classes full, not Classes , the same problem with traits: Traits do not work, but Test\Traits very even.

1 answer 1

The point is that you placed the class Someclass on the path test \ classes \ someclass.php relative to the calling point in index.php. In principle, you can do this — this is not prohibited — that is, it works, however, you have to set your own autoload rule for this class in spl_autoload_register() .

If you want the autoloader to automatically detect your class, you will have to place it in the Test\Classes namespace. Moreover, the PSR-4 standard simply instructs you to use paths in the namespace that match the physical path to the file with the class.