代码如下:
index.php
<?php use App\Route; use App\Controller\HomeController; define('BASEDIR', __DIR__); include BASEDIR . '/App/Loader.php'; spl_autoload_register('\\App\\Loader::autoload'); HomeController::index(); call_user_func('HomeController::index');
App\Controller\HomeController.php
<?php namespace App\Controller; class HomeController{ public static function index(){ echo __METHOD__; } }
App\loader.php
<?php namespace App; class Loader{ public static function autoload($class){ $class = str_replace('\\', '/', $class); require BASEDIR . '/' .$class . '.php'; } }
运行结果:
App\Controller\HomeController::index
Warning: require(D:\wwwroot\htdocs\dd/HomeController.php): failed to open stream: No such file or directory in D:\wwwroot\htdocs\dd\App\Loader.php on line 10
Fatal error: require(): Failed opening required 'D:\wwwroot\htdocs\dd/HomeController.php' (include_path='.;C:\php\pear') in D:\wwwroot\htdocs\dd\App\Loader.php on line 10我知道写成call_user_func('App\Controller\HomeController::index');是可以解决问题的。
但是采用简单的call_user_func('HomeController::index');写法,应该怎么解决
index.php没定义BASEDIR
index.php没定义BASEDIR