Jump to content

Using namespaces with dynamic variables


9three

Recommended Posts

Hey,

 

Instead of using a switch statement like on the Advanced PHP video, I'm doing this:

 

public function route() {

$controllerClass = 'application/controllers/' .$this->getController(). 'Controller.php';

$className = $this->getController().'Controller';

 

if (file_exists($controllerClass))

return new $className($this->getController(), $this->getAction());

else

return new errorController($this->getController(), $this->getAction());

}

 

As you can see, the $className stores the actual class name. The problem is that I can't use namespaces when I do this.

 

Controller\$className(); -- returns a syntax error. Not sure if this is something that still needs to get fixed as I know namespaces are still new to PHP.

 

Or does someone know a workaround? :)

Link to comment
Share on other sites

  • 2 months later...

I run into the same problem. Try using the full namespace as a class name. Do not use an alias because it will not work. Use the full call.

 

i.e.

$classname = 'application\controllers\\'.$this->getController().'Controller' ;

 

return new $classname($this->getController(), $this->getAction());

 

 

It worked for me

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...