Jump to content

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/10555-object-efficiency/
Share on other sites

[!--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.
Link to comment
https://forums.phpfreaks.com/topic/10555-object-efficiency/#findComment-39383
Share on other sites

  • 3 weeks later...
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?
Link to comment
https://forums.phpfreaks.com/topic/10555-object-efficiency/#findComment-46623
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.