RoBiNp Posted June 20, 2009 Share Posted June 20, 2009 This works normally: $nameOfClass = 'SomeExistingClass'; // class name $obj = new $nameOfClass(); echo $obj->someExistingFunction(); // display class I want to use __construct() but changing someExistingFunction() to __construct() causes for some mysterious reason double mysql queries, double print_r() and double echo (function returns behave normally). So how should I call __construct() ? Quote Link to comment https://forums.phpfreaks.com/topic/163029-how-to-call-__construct-in-a-class-by-a-variable/ Share on other sites More sharing options...
Daniel0 Posted June 20, 2009 Share Posted June 20, 2009 You usually shouldn't call it manually. It's called upon instantiation automatically. Quote Link to comment https://forums.phpfreaks.com/topic/163029-how-to-call-__construct-in-a-class-by-a-variable/#findComment-860178 Share on other sites More sharing options...
RoBiNp Posted June 20, 2009 Author Share Posted June 20, 2009 Yes, but where should I put the echo then? (I'm using return in __construct() ) Quote Link to comment https://forums.phpfreaks.com/topic/163029-how-to-call-__construct-in-a-class-by-a-variable/#findComment-860199 Share on other sites More sharing options...
Daniel0 Posted June 20, 2009 Share Posted June 20, 2009 You cannot return a value from the constructor. You can use the __toString() magic method though. When you try to echo an object (or otherwise represent the object as a string), that function will be called. Quote Link to comment https://forums.phpfreaks.com/topic/163029-how-to-call-__construct-in-a-class-by-a-variable/#findComment-860204 Share on other sites More sharing options...
RoBiNp Posted June 20, 2009 Author Share Posted June 20, 2009 Ah, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/163029-how-to-call-__construct-in-a-class-by-a-variable/#findComment-860211 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.