3

Trying to understand how namespace in php works and am stuck.

This below is the architecture of the project.

enter image description here

Class: Loader.php (suppose to load controller/model/ library but for now gibberish test codes)

namespace system\core;

class Loader
{
    public function index()
    {
        echo 'loader';
    }

    public function controller($pathtocontroller)
    {
        // Echo path to the controller
        echo $pathtocontroller;

    }
}

index.php

require 'system/core/Loader.php';
require 'system/core/BaseController.php';
require 'app/controller/common/HomeController.php';

use system\core;
use app\controller;

$loader = new \system\core\Loader();
$loader->controller(app\controller\common\HomeController);

and this is the error I get

Fatal error: Undefined constant 'app\controller\common\HomeController' in C:\xampp\htdocs\psrstd\index.php on line 20. Lin 20 on index is $loader->controller(app\controller\common\HomeController);

Expected Result: app/controller/common/HomeController

in case you wondering what's in there at the HomeController (again gibberish test code)

namespace app\controller\common;
use system\core\BaseController;

class HomeController extends BaseController
{
    public function index()
    {
        echo 'home';
    }
}
5
  • What are you tring to achieve? Commented Nov 12, 2017 at 10:51
  • @u_mulder, the HomeController is at app/controller/common. I was simply trying to echo the path to the controller. Commented Nov 12, 2017 at 10:54
  • Why do you need path to the controller? Commented Nov 12, 2017 at 10:55
  • @u_mulder, load the controller like so $this->load->controller('common/HomeController'); .. Commented Nov 12, 2017 at 10:59
  • @u_mulder, but that is later ... Loader.php is supposed to be a class loader. Commented Nov 12, 2017 at 10:59

1 Answer 1

1

you are passing a constant type to your controller method,

you are aiming to pass an object [ dependency injection ] so you will need to instantiate your argument like :

$loader->controller(new \app\controller\common\HomeController);

otherwise you may send this argument as a string like :

$loader->controller("\\app\\controller\\common\\HomeController");

and instantiate that object within your method [ as a factory method ]

public function controller($pathtocontroller)
{
    // new $pathtocontroller and so on

}

Further reading :

What is Dependency Injection?

Factory design pattern

Sign up to request clarification or add additional context in comments.

4 Comments

this worked $loader->controller("\\app\\controller\\common\\HomeController");", but looks ugly. Is there a better way?
what do you mean by looks ugly?
\\app\\controller\\common\\HomeControll‌​er .. do I need to '\\' ? My old project I use to do $this->load->controller(common/HomeController) now having to do this $this->load->controller(common\\HomeController) would look ugly ... I'll do it if there is no other way but generally asking ... can it be done any better?.
the backslash is the former separator for namespaces in PHP, you can not git rid of it, and since the backslash is also the escaping character so you must escape it like this `\`

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.