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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • 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.