PHP 中的命名空间和 set_include_path() 存在问题
C:\xampp\htdocs 包含 Controller.php 和 ApplicationHelper.php。 C:\xampp\htdocs\site 包含 index.php。
这是我收到的错误:
致命错误:在第 17 行的 C:\xampp\htdocs\Controller.php 中找不到类 'site\controller\ApplicationHelper'
我对整个命名空间很陌生但我不能 100% 确定这就是其背后的原因。即使我设置了包含路径来查找该文件夹,它似乎也找不到 ApplicationHelper.php 。如果我直接将 ApplicationHelper.php 包含在 Controller.php 中,它就可以工作。这是(相关)代码:
index.php
set_include_path(get_include_path() . PATH_SEPARATOR . 'C:\xampp\htdocs');
require('Controller.php');
\site\controller\Controller::run();
Controller.php
namespace site\controller;
class Controller {
private $applicationHelper;
private function __construct () {}
static function run () {
$instance = new Controller();
$instance->init();
}
function init () {
$applicationHelper = ApplicationHelper::instance();
$applicationHelper->init();
}
}
ApplicationHelper.php
namespace site\controller;
class ApplicationHelper {
private static $instance;
private function __construct () {}
static function instance () {
if (!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
function init() {
}
}
感谢您的帮助!
C:\xampp\htdocs contains Controller.php and ApplicationHelper.php. C:\xampp\htdocs\site contains index.php.
Here is the error I am getting:
Fatal error: Class 'site\controller\ApplicationHelper' not found in C:\xampp\htdocs\Controller.php on line 17
I'm new to the whole namespaces business but I'm not 100% sure that thats whats behind it. It just seems like its not finding ApplicationHelper.php even though I set the include path to look in that folder. It works if I directly include ApplicationHelper.php in Controller.php. Here is the (relevant) code:
index.php
set_include_path(get_include_path() . PATH_SEPARATOR . 'C:\xampp\htdocs');
require('Controller.php');
\site\controller\Controller::run();
Controller.php
namespace site\controller;
class Controller {
private $applicationHelper;
private function __construct () {}
static function run () {
$instance = new Controller();
$instance->init();
}
function init () {
$applicationHelper = ApplicationHelper::instance();
$applicationHelper->init();
}
}
ApplicationHelper.php
namespace site\controller;
class ApplicationHelper {
private static $instance;
private function __construct () {}
static function instance () {
if (!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
function init() {
}
}
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要包含 ApplicationHelper.php 或使用自动加载器。
You need to include ApplicationHelper.php or use an autoloader.
请参阅:http://php.net/manual/en/language.oop5 .autoload.php
See this: http://php.net/manual/en/language.oop5.autoload.php