문제

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!

도움이 되었습니까?

해결책

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

function __autoloader($class_name)
{
    include  $class_name . "php";
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top