jajtiii Posted May 27, 2006 Share Posted May 27, 2006 I am building a script that utilizes a few master class objects and a lot of child class objects. Effectively, the child objects are only called when necessary, whereas the parent objects are always instantiated.My question revolves around 'efficiency'. There are some variables that exist within a parent class (let's say, $myLogin->user_id) that are utilized within the child objects. Currently, when I need to access the user_id, I simply set the object instance as global and use it directly from the instantiated object. Such as :<code>class MyMonkey{function loadParams() { global $myLogin; $sql = 'SELECT params FROM params_table WHERE user_id = "'.$myLogin->user_id.'"'; ....etc....}</code>In many instances, the class above will have several methods that do something similar (globalize $myLogin and use the variables like above.) Although possibly trivial, I am wondering if it would be more effecient to simply make the variable local in the constructor of the child objects and not have to globalize the original object each time.For example:<code>class MyMonkey { public $user_id; __construct() { global $myMonkey; $this->user_id = $myLogin->user_id; }}</code>In this example, each subsequent use of 'user_id' by other methods of this class would only need to use $this->user_id. When I built the script, I just assumed that doing this would mean the need to allocate a memory space for an additional variable, which would not be efficient. But, now I am beginning to wonder exactly how PHP reacts to globalizing an object. Does it actually create the entire object, each time it is globalized, into a another memory space? This would make my method inefficient.So, thought I'd ask the experts.Thanks in advance for your assistance.jt Quote Link to comment https://forums.phpfreaks.com/topic/10555-object-efficiency/ Share on other sites More sharing options...
hvle Posted May 27, 2006 Share Posted May 27, 2006 [!--quoteo(post=377482:date=May 27 2006, 01:18 PM:name=jajtiii)--][div class=\'quotetop\']QUOTE(jajtiii @ May 27 2006, 01:18 PM) [snapback]377482[/snapback][/div][div class=\'quotemain\'][!--quotec--]I am building a script that utilizes a few master class objects and a lot of child class objects. Effectively, the child objects are only called when necessary, whereas the parent objects are always instantiated.My question revolves around 'efficiency'. There are some variables that exist within a parent class (let's say, $myLogin->user_id) that are utilized within the child objects. Currently, when I need to access the user_id, I simply set the object instance as global and use it directly from the instantiated object. Such as :<code>class MyMonkey{function loadParams() { global $myLogin; $sql = 'SELECT params FROM params_table WHERE user_id = "'.$myLogin->user_id.'"'; ....etc....}</code>In many instances, the class above will have several methods that do something similar (globalize $myLogin and use the variables like above.) Although possibly trivial, I am wondering if it would be more effecient to simply make the variable local in the constructor of the child objects and not have to globalize the original object each time.For example:<code>class MyMonkey { public $user_id; __construct() { global $myMonkey; $this->user_id = $myLogin->user_id; }}</code>In this example, each subsequent use of 'user_id' by other methods of this class would only need to use $this->user_id. When I built the script, I just assumed that doing this would mean the need to allocate a memory space for an additional variable, which would not be efficient. But, now I am beginning to wonder exactly how PHP reacts to globalizing an object. Does it actually create the entire object, each time it is globalized, into a another memory space? This would make my method inefficient.So, thought I'd ask the experts.Thanks in advance for your assistance.jt[/quote]This is a good question, I would want to know myself. In my opinion, you can not think php as C/C++. PHP load the entired script, run it and dump it. If you are concern about memory when global a variable. An alternative way is passing that variable by reference. With this way, you hold only the address of global variables, and not worry about copy the entire structure.Sorry I'm not an expert. I'm just interested in this issue. Hope you don't mind me posting. Quote Link to comment https://forums.phpfreaks.com/topic/10555-object-efficiency/#findComment-39383 Share on other sites More sharing options...
jajtiii Posted May 28, 2006 Author Share Posted May 28, 2006 Can anyone suggest a forum where topics of this nature are discussed? Quote Link to comment https://forums.phpfreaks.com/topic/10555-object-efficiency/#findComment-39669 Share on other sites More sharing options...
jajtiii Posted June 17, 2006 Author Share Posted June 17, 2006 Ok, here is another one.I was upgrading a class object script of mine the other day and noticed that I had made an error in my last tweak to this object. I had never declared one of the variables in the class object.Oddly, PHP was still able to pull the variable out of the object.Does PHP basically create memory references within an object on the fly? Quote Link to comment https://forums.phpfreaks.com/topic/10555-object-efficiency/#findComment-46623 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.