PHP memory implication on increasing the number of Global variables.

Events happening in the community are now at Drupal community events on www.drupal.org.
navaneeth_r's picture

Creating Related links todispaly with in the comment.tpl

We are repositioning our related links custom block form ( result fro a view, by passing taxonomy as argument) within node.tpl to comment.tpl. We want to store the taxonomy id for the node in the global variable and use it to generate related links in the comment.tpl section by passing this taxonomy global variable to the view. This is instead of reloading the node to obtaining the taxonomy id.

Our concern is the Memory used by the Global variable. The global variable as I understand in PHP gets cleared when the script ends. We will in this case be generating PHP variable for every node to store the taxonomy id and use it across the page to display various information. The advantage is to prevent multiple node loads, which is very expensive in a large site with many nodes. But I not sure of the PHP memory implication on increasing the number of Global variables.

Comments

Memory implications can be

berenddeboer's picture

Memory implications can be ignored. It's as you said: when script end, memory is cleaned up. Keeping track of a few things won't really increase your memory usage. The only thing you should be asking is do you really understand Drupal? Global variables are not really necessary, I have never had occasion to use this, so perhaps you are using Drupal contrary to how you should be using it.

Node_load caches the node

John Hodgins's picture

Node_load caches the node object as a static variable -- reloading the node in other parts of the page is no additional cost.

Static Variables

joshk's picture

The "drupal way" here is to use static variables rather than globals when possible. This keeps things relatively clean.

As mentioned above, the $node object is statically cached, so you can do a node_load() in your comment space at extremely little cost.

http://www.chapterthree.com | http://www.outlandishjosh.com

*_*

beautifulmind's picture

There is another way out if you do not want all the variables from $node.
Just write a query with specific fields -- these fields should be those which will be used subsequently, no extra field.
Load the result in an object and you are with your data.

Regards.
:)
Beautifulmind

High performance

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: