0

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!

2 Answers 2

1

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

function __autoloader($class_name)
{
    include  $class_name . "php";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Would it be preferred to place that in index.php or each class that uses it?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.