Jump to content

Dynamic Class Creation Calls


andygrant

Recommended Posts

Hi

 

I have a function to create objects dynamically and I have seen you can use variables for the new xxx part, however the objects I'm creating require different amounts of paramaters sent to them on creation.

 

I attempted to do this by sending the class name and an array of the parameters, then using implode to split the parameters array by a comma and it sort of creates what I wanted, but its only being recognised as one string paramter rather than multiple comma seperated parameters.

 

new $objectName(implode(', ', $parameters));

 

The code I tried is shown above, does anyone have an idea how I could best achive this. I could probably do it putting the entire line into an eval() but I'd rather avoid this if someone has a better idea.

 

Many Thanks

Link to comment
Share on other sites

Generic's reflection should work as intended, however I would urge you to consider using arrays or objects (in the form of a struct). Also if you're doing dynamic variable creation, as this is the OOP part of the forum I recommend you use interfaces/abstracts to define your base implementations for your classes.

 

Can you show us some code so that we may piece together a more OO layout for you?

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.