[wp-hackers] Widget Coding Standard

Jacob Santos wordpress at santosj.name
Fri Jan 25 04:10:08 GMT 2008


Is there some reason why widget coders place the widget display code in 
the widget initialize function? I was contemplating that and came to the 
conclusion that someone, Person A, decided it would be cool to place 
functions inside of functions like a closure, because they could. I 
believe that Person B, while trying to learn how to write widgets 
decided that the idea was cool and probably that was how it worked and 
copied the idea.

As many widgets (I've seen three, but those are pretty good odds that 
enough people are infected with this idea, but not enough to get the 
full picture of how many coders actually do this), as I've seen, I think 
the idea is great, but should be shun in practice.

At some point, PHP might (unlikely, but granted there are patches that 
allow this, so it might be a matter of time), at some point implement 
true closure support and woohoo, the widgets will magically stop working 
on that PHP version.

The way it works now, is that PHP sees the function declaration and 
pulls the function to the global space and compiles it. If the patches 
are applied, then the function will be created anonymously to the widget 
init function and gain the variable scope of the parent function. This 
is probably not so bad, in and of itself, because most widget init 
functions don't do much, except register the widget display function.

The question I have is, why are so many widget developers using this 
method? PHP allows it and will continue to allow it, but the widget 
display functions are not closures and should not be developed in such a 
way that they are. The only thing it prevents is the compilation of the 
function until the init function is called. I do not believe there is 
any difference between compiling it when the file is pulled in by PHP 
and during runtime when PHP sees that the function needs to be compiled. 
The function will still exist around the same time.

Does anyone have any rebuttal or authority to tell these people to stop? 
I know you can't prevent them, but I plan to write about it and right 
now I'll very much sound like an ass. I don't wish to, because I already 
sound like one in this email.

-- 

Jacob Santos

http://www.santosj.name - blog
http://funcdoc.wordpress.com - WordPress Documentation Blog/Guide Licensed under GPLv2

Also known as darkdragon and santosj on WP trac.



More information about the wp-hackers mailing list