Warning: Cannot use a scalar value as an array in /home/admin/public_html/forum/include/fm.class.php on line 757

Warning: Invalid argument supplied for foreach() in /home/admin/public_html/forum/include/fm.class.php on line 770
Форумы портала PHP.SU :: Версия для печати :: namespace/autoload
Форумы портала PHP.SU » » Вопросы новичков » namespace/autoload

Страниц (1): [1]
 

1. mstan - 13 Июля, 2017 - 12:44:41 - перейти к сообщению
Разбираюсь с аутолоадом и пространствами имен.
Есть два файла:
index.php
./classes/live/Human.php

index.php
PHP:
скопировать код в буфер обмена
  1. <?PHP
  2. function Myautoloader($class)
  3. {
  4.     $class = str_replace('\\', '/', $class).'.php';
  5.     if(file_exists($class)){
  6.         echo 'file found';
  7.         require_once $class;
  8.     }
  9. }
  10. spl_autoload_register('Myautoloader');
  11.  
  12. use classes\live\Human;
  13.  
  14. $human = new Human();
  15.  
  16.  


Human.php
PHP:
скопировать код в буфер обмена
  1. <?PHP
  2. namespace classes\live\Human;
  3.  
  4. class Human
  5. {
  6.     public function __construct()
  7.     {
  8.         echo "Class human created";
  9.     }
  10.  
  11. }


Вывод в браузере
Цитата:
file found
Fatal error: Uncaught Error: Class 'classes\live\Human' not found in D:\xampp7.1\htdocs\php_ess\index.php:14 Stack trace: #0 {main} thrown in D:\xampp7.1\htdocs\php_ess\index.php on line 14


Как я понимаю, пространство имен срабатывает, файл находится, но почему же он не инклюдится?



UPD Решено, namespace classes\live;

 

Powered by ExBB FM 1.0 RC1