djones Posted December 31, 2009 Share Posted December 31, 2009 What is the best way to accomplish this? I need to reuse a class inside a function. Is it ok on performance to intialize the class again in the function? See below. $oClass = new cClass(); // ... Use class for tasks ... // Use class in function without passing $oClass // Want to do this without having to initialize the class again. OR is this OK? function nav($id){ // Would not like to initialize class again. // $oClass = new cClass() // do stuff with $oClass } // I do not want to do this function nav($id, $oClass)[ // Do stuff with $oClass } Quote Link to comment https://forums.phpfreaks.com/topic/186756-using-an-initialzed-class-inside-a-funtion-without-using-global/ Share on other sites More sharing options...
trq Posted December 31, 2009 Share Posted December 31, 2009 If the function requires the object the best thing to do is pass it in as an argument. Why don't you want to? Quote Link to comment https://forums.phpfreaks.com/topic/186756-using-an-initialzed-class-inside-a-funtion-without-using-global/#findComment-986209 Share on other sites More sharing options...
premiso Posted December 31, 2009 Share Posted December 31, 2009 One reason he may not want to (which I am not 100% sure happens) is passing it as an argument creates a new object? You may want to try and make the class global global $oClass; $oClass = new class(); function new($id) { global $oClass; } However, like I said, I am not sure if this is what you want or if it is considered good practice, but it is another option for you. Quote Link to comment https://forums.phpfreaks.com/topic/186756-using-an-initialzed-class-inside-a-funtion-without-using-global/#findComment-986219 Share on other sites More sharing options...
trq Posted December 31, 2009 Share Posted December 31, 2009 One reason he may not want to (which I am not 100% sure happens) is passing it as an argument creates a new object? No it doesn't. Objects are passed by reference by default. Quote Link to comment https://forums.phpfreaks.com/topic/186756-using-an-initialzed-class-inside-a-funtion-without-using-global/#findComment-986220 Share on other sites More sharing options...
djones Posted December 31, 2009 Author Share Posted December 31, 2009 The reason why I do not want to pass it in the function is because the function will be a configuration function for a user, I was trying to make is as clean as possible. I did not want the user to have to worry about what the other variable was for. // easier for user function nav($id){} // Do not wish todo function nav ($id, $oClass){} With that, is a global safe? Or should I re-initialize the class again in the function? Quote Link to comment https://forums.phpfreaks.com/topic/186756-using-an-initialzed-class-inside-a-funtion-without-using-global/#findComment-986223 Share on other sites More sharing options...
trq Posted December 31, 2009 Share Posted December 31, 2009 globals are considered bad practice because they tie your function to an outside variable/object. If your function truthfully requires this object it may be best to instantiate it within the function. Quote Link to comment https://forums.phpfreaks.com/topic/186756-using-an-initialzed-class-inside-a-funtion-without-using-global/#findComment-986237 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.