No symbolic link is created when using use.

file index.php

 use \Folder\Aa; Aa::test(); $test = new Aa(); 

file Folder/Aa.php

 namespace Folder; class Aa { static function test() { $a = 3; echo $a; } } 

Throws an error

Fatal error: Class 'Folder \ Aa' not found in /home/ademidko/www/first.local/index.php

I changed the namespace Аа.php , tried it like "use \Folder" , etc., but it still does not work.

  • one
    And how do you connect Аа.php to index.php ? - Ostin
  • When you connect the file through include, for example, everything works. But in the manual php.net/manual/ru/language.namespaces.importing.php it is written that a symbolic link to the file should be created - Deniden
  • Using use does not release you from having to attach a file. - Dmitriy Simushev

1 answer 1

Most likely, the problem is that you do not have a class loader defined. Try to declare an autoloader in index.php

Something like

 spl_autoload_register(function ($class) { // project-specific namespace prefix $prefix = 'Foo\\Bar\\'; // base directory for the namespace prefix $base_dir = __DIR__ . '/src/'; // does the class use the namespace prefix? $len = strlen($prefix); if (strncmp($prefix, $class, $len) !== 0) { // no, move to the next registered autoloader return; } // get the relative class name $relative_class = substr($class, $len); // replace the namespace prefix with the base directory, replace namespace // separators with directory separators in the relative class name, append // with .php $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php'; // if the file exists, require it if (file_exists($file)) { require $file; } });