0

I was studying a couple PHP frameworks and then decided to build my own, of course. But i'm facing one issue. I have a Router class that handles dynamically the HTTP requests and it basically explodes the URL into elements dividing it by the slash and storing it into an array, then a function is called to check if the first element is a valid Controller. If it is valid, the function should require it, but that's where i'm stuck, because it seems that i can't require a file like:

if (file_exists(CONTROLLERS_DIR . $this->url[0] . '.php')) { require \App\Controllers\$this->url[0] }

How can I require a file like that using namespaces?

Thanks.

6
  • if (file_exists(CONTOLLERS_DIR . $this->url[0])) { require CONTOLLERS_DIR . $this->url[0] } ? Commented Oct 1, 2015 at 14:29
  • What error are you getting? Have you checked the value of $this->url[0]? Is it correct? Commented Oct 1, 2015 at 14:29
  • Actually, i'm using an autoloader, so i don't need to check if the file exists. I just need to require it. I just used it here to simply explain what i'm going though...by the way, the error i got is: 'unexpected $this after / should be identifier'. Commented Oct 1, 2015 at 14:33
  • But i still cant do this: new \App\Controllers\$this->url[0]; Commented Oct 1, 2015 at 14:39
  • You cannot perform a string concat in a require/include statement. $ctrl = '\\App\\Contrllers\\'.$this->url[0]; $c = new $ctrl; oh you gotta escape your slashes too. see here for how i accomplished this: github.com/r3wt/RedBeanFVM/blob/master/RedBeanFVM/… Commented Oct 1, 2015 at 14:45

2 Answers 2

2

"How can I require a file like that using namespaces?"
You can't. Namespaces have nothing to do with it.

"PHP Namespaces provide a way in which to group related classes, interfaces, functions and constants." ~ Namespaces overview


require is about file dependancies, regardless the namespace:

if (file_exists(CONTROLLERS_DIR . $this->url[0] . '.php')) { 
    require(CONTROLLERS_DIR . $this->url[0] . '.php');
}

EDIT: You might want to instantiate a class using a namespace and class name retrieved in run-time though, i.e. something like:

namespace \App\Controllers;
class C {
    protected $_i;
    public function __construct($i){ $this->_i = $i; }
    public function foo(){ echo $this->_i; }
}

and somewhere:

$className = "C";                   // or $className = $this->whatever...
$class = "\\App\\Controllers\\".$className;
$instance = new $class(7);
$instance->foo();                   // outputs 7
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't answer the question. Edit: maybe it does but its still not concise enough of an answer. please revise.
@r3wt: I agree, have a look now :)
0

I've built couple of frameworks and I understand what you trying to do... Basically when you have some path for example "HelloWorld\addComment"

You want to create controller instance

\App\Controllers\HelloWorldController

There are multiple ways to solve it, the one I like is:

Using spl autoloader http://php.net/manual/en/function.spl-autoload.php

In the link I provided you got the examples you need.

Then you can end up just doing

$controller = new \App\Controllers\HelloWorldController();

You should put the HelloWorldController at the right namespace + maintain directory structure matching the namespace

app     
   Controllers
       HelloWorldController

spl autoloader will do the right including for you, often the default implementation is sufficient - but it is easy to create your own spl autoloader and register it

Later you can test if the $controller has the method you need via method_exist or reflection...

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.