У меня проблема с классами. Я использую https://www.php -fig.org / psr / psr-4 / для классов автозагрузки.
------------------------------------ /home/classes/a/casea.php
namespace Classes\A\CaseA;
class ClassA {
public function MethodA()
{
$resultA = 'Method A works!';
return $resultA;
}
}
------------------------------------ /home/classes/b/caseb.php
namespace Classes\B\CaseB;
class ClassB {
public function MethodB()
{
// Classes\A\CaseA\ClassA MethodA() don't work here.
// I tried this, but didn't work.
$obj = new Classes\A\CaseA\ClassA;
$result = $obj->MehodA();
return $result;
}
}
$classb = new Classes\B\CaseB\ClassB;
$show = $classb->MethodB();
echo $show;
------------------------------------ Автозагрузка
## PHP-FIG Autoload
spl_autoload_register(function ($class) {
// project-specific namespace prefix
$prefix = 'Classes\\';
// base directory for the namespace prefix
$base_dir = PATH . '/classes/';
// 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 . strtolower(str_replace('\\', '/', $relative_class)) . '.php';
// if the file exists, require it
if (file_exists($file)) {
require $file;
}
});
Ошибка в следующем тексте.
Fatal error: Uncaught Error: Class 'Classes\B\Classes\A\ClassA' not found in /home/domain.tld/classes/b/classb.php:11 Stack trace: #0