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
https://forums.phpfreaks.com/topic/126789-dynamic-class-creation-calls/
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?

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.