PDA

View Full Version : Do not use $i in global code!


JBJBJB
12-27-2005, 12:09 PM
Hello to all,

I just found the answer to a very strange problem I encountered several times. The problem was that Subdreamer started to repeat the rendering of plugins (so if I had 2 plugins defined on a page, then after the second plugin, it started to render the first plugin again etc...)
That caused problems with duplicate includes.

After about three weeks and reviewing my code multiple times, I suddenly realized that I use a variable $i in a loop which was not in a subroutine. So the scope of the $i would also be global. After I renamed the $i to something else, the repeating disappeared!!

Most certainly, the Subdreamer code to loop the plugins on a page, also uses $i as the counter for a loop!
I hope this is helpfull for others as well,

Sincerely,
Jeroen

spib
12-28-2005, 09:07 AM
The way plugins are rendered is down to the skin, not Subdreamer itself but the default Advanced skin does use $i as a counter when rendering the plugins.

Good catch :)

JBJBJB
12-28-2005, 10:56 AM
<div class='quotetop'>QUOTE(spib &#064; Dec 28 2005, 11&#58;07 AM) 2588</div>
The way plugins are rendered is down to the skin, not Subdreamer itself but the default Advanced skin does use &#036;i as a counter when rendering the plugins.

Good catch :)
[/b]

Thanks for pointing that out. I think it&#39;s something especially a default skin SHOULD NOT DO&#33; It should use specialized names global variables in order to prevent conflicts like these. Perhaps a best-practice for anyone writing plugins.

You can argue that plugin developers shouldn&#39;t use &#036;i as well for the same reason but I think that at the top of the food-chain, so to speak, it should be done right since it has the biggest impact and can shield less-experienced programmers from weird problems like I encountered. I&#39;ll send a message to Subduck about it.

Since I&#39;m busy integrating two products, I should not change code of either of them so I worked around it by putting a backup/restore for &#036;i around the code. This works fine in my situation.