From: Verdon V. <ve...@ve...> - 2006-03-09 22:53:46
|
Hi, I've been noticing while reading through some of the fallout and modules code the frequent use of references / var references (&). By this I mean things like the following examples... 1) $search = & new Search($this->key_id); 2) function &cpanel() { do something; } 3) function _loadCategory(&$cat_item, &$blog, $version=NULL) { do something; } Doing a little reading at php.net explains that these (&) are references to the original (think hard link) and may be used for a couple reasons. I don't really have a grip on this yet, but I'll try to explain what I think I understand, and then ask a question. Reason 1 ========== $a = 5; $b = $a; $b = $b -1; In this case, b is a copy of a and the final result is $b is equal to 4 and $a is equal to 5. $a = 5; $b = &$a; $b = $b -1; In this case, b is a reference to a and the final result is $b is equal to 4 and $a is equal to 4. Reason 2 ========== Apparently it can speed up / optimize some internal functions of the php compiler to have operations done to one object in memory instead of on one or more copies of it. So, with that little bit of knowledge, I think I understand what is going on in example 3, but I'm not quite sure about example 1 or 2. Is there anybody who can shed a little light on this for me and when I should be using the same style? Thanks, verdon |