the_oliver Posted November 22, 2010 Share Posted November 22, 2010 Hi, I need to be able to call a class based on variables. E.G. I would normally do: $action = new pattern1() but i would like to be able to do it dynamicaly: $patNum = 1; $action = new pattern.$patNum.() Im wondering if that's possible? If so what would the correct syntax be? Many Thanks. Link to comment https://forums.phpfreaks.com/topic/219454-calling-class-when-class-name-is-build-from-variables/ Share on other sites More sharing options...
MrXHellboy Posted November 22, 2010 Share Posted November 22, 2010 I guess concatenation will not work with instances. You can create 2 separate methods and use a if construct to call the methods. Link to comment https://forums.phpfreaks.com/topic/219454-calling-class-when-class-name-is-build-from-variables/#findComment-1137897 Share on other sites More sharing options...
GKWelding Posted November 22, 2010 Share Posted November 22, 2010 If I remember rightly, this will work... $className = 'pattern'.$patNum; $action = new $className(); Link to comment https://forums.phpfreaks.com/topic/219454-calling-class-when-class-name-is-build-from-variables/#findComment-1137899 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.