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. Quote 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. Quote 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(); Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.