Jump to content

Dynamic namespaced class instantiation


ddubs

Recommended Posts

So I'm pounding away at some of the new 5.3 features to try and get a grasp on setting up a custom MVC. spl_autoload with namespaces is frikkin awesome, however im trying to make life easy by being able to drop a class file in an "objects" folder that holds all the models for the app and have the controller parse out the get request for the proper model call and dynamically create the object. Example:

 

url = www.example.com/?auth

 

controller parses $_GET and see's you are requesting the auth module. Now I'm having an issue instantiating a variable that has a namespace\class inside it:

 

$namespace = 'objects\\';
$clean['object'] = $namespace.key($clean['get']);
$this->object = new $clean['object'];

 

Logfile:

PHP Fatal error:  spl_autoload(): Class objects\\testobject could not be loaded...

 

OK, its trying to escape the backslash? Maybe some trickery is needed?

 

$clean['object'] = $namespace.chr(92).key($clean['get']);

 

NOPE, still the same error... Any cluepons?

Link to comment
https://forums.phpfreaks.com/topic/250450-dynamic-namespaced-class-instantiation/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.