开发者

Having a problem with namespaces and set_include_path() in PHP

开发者 https://www.devze.com 2023-01-13 06:54 出处:网络
C:\\xampp\\htdocs contains Controller.php and ApplicationHelper.php.C:\\xampp\\htdocs\\site contains index.php.

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('Controll开发者_C百科er.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!


You need to include ApplicationHelper.php or use an autoloader.

function __autoloader($class_name)
{
    include  $class_name . "php";
}


See this: http://php.net/manual/en/language.oop5.autoload.php

0

精彩评论

暂无评论...
验证码 换一张
取 消