andygrant Posted October 2, 2008 Share Posted October 2, 2008 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 More sharing options...
genericnumber1 Posted October 2, 2008 Share Posted October 2, 2008 Try $reflection = new ReflectionClass($objectName); $object = $reflectionObj->newInstanceArgs($parameters); Link to comment https://forums.phpfreaks.com/topic/126789-dynamic-class-creation-calls/#findComment-655843 Share on other sites More sharing options...
aschk Posted October 3, 2008 Share Posted October 3, 2008 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 https://forums.phpfreaks.com/topic/126789-dynamic-class-creation-calls/#findComment-656198 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.