<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Global variables in WordPress</title>
	<atom:link href="http://mgeisler.net/2005/05/global-variables-in-wordpress/feed/" rel="self" type="application/rss+xml" />
	<link>http://mgeisler.net/2005/05/global-variables-in-wordpress/</link>
	<description>Adventures with Computers</description>
	<pubDate>Thu, 02 Sep 2010 20:12:26 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6-bleeding</generator>
		<item>
		<title>By: John Wells</title>
		<link>http://mgeisler.net/2005/05/global-variables-in-wordpress/#comment-382484</link>
		<dc:creator>John Wells</dc:creator>
		<pubDate>Mon, 01 Feb 2010 02:30:04 +0000</pubDate>
		<guid isPermaLink="false">http://mgeisler.net/2005/05/24/global-variables-in-wordpress/#comment-382484</guid>
		<description>&lt;p&gt;This has been the biggest problem I have faced in writing the WP-United integration. When embedding multiple pieces of software, I naturally want to write OO, or at least, compartmentalised code, but WordPress relies too much on variables being in the global scope.&lt;/p&gt;

&lt;p&gt;As it is now, I end up having to use a painful combination of globalising long lists of variables, and preparing all code paths in functions and class members, but eval()ing it in the global scope.&lt;/p&gt;

&lt;p&gt;There are still some instances where I really do have to run the code in a function scope -- it is just too swkward to bend the code into the global scope, but despite tracking WordPress' globals with each iteration, it turns out that the majority of plugins are written assuming that they themselves are in the global scope.&lt;/p&gt;

&lt;p&gt;Then you have to clean up the mess they make afterwards.&lt;/p&gt;

&lt;p&gt;The problem is that none of this can be undone -- with tens of thousands of plugins and themes all working on the assumption that they are in the global scope, nothing will ever be able to wrap WordPress 100% cleanly and reliably.&lt;/p&gt;

&lt;p&gt;WordPress in general has excellent, clean and easy to follow code, but this was a decision that has sent them down the wrong path. Forcing all plugins and themes into a function scope, while more painful initially, would have completely removed all of these issues.&lt;/p&gt;

&lt;p&gt;Right now I have even resorted to "compiling" plugins -- parsing the files before they are executed, grepping all the "global xxx" definitions, and proactively making them global before they are defined. Huge pain, but necessary if I want wordpress to run at specific points in a third-party application -- e.g. during user authentication.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>This has been the biggest problem I have faced in writing the WP-United integration. When embedding multiple pieces of software, I naturally want to write OO, or at least, compartmentalised code, but WordPress relies too much on variables being in the global scope.</p>
<p>As it is now, I end up having to use a painful combination of globalising long lists of variables, and preparing all code paths in functions and class members, but eval()ing it in the global scope.</p>
<p>There are still some instances where I really do have to run the code in a function scope &#8212; it is just too swkward to bend the code into the global scope, but despite tracking WordPress&#8217; globals with each iteration, it turns out that the majority of plugins are written assuming that they themselves are in the global scope.</p>
<p>Then you have to clean up the mess they make afterwards.</p>
<p>The problem is that none of this can be undone &#8212; with tens of thousands of plugins and themes all working on the assumption that they are in the global scope, nothing will ever be able to wrap WordPress 100% cleanly and reliably.</p>
<p>WordPress in general has excellent, clean and easy to follow code, but this was a decision that has sent them down the wrong path. Forcing all plugins and themes into a function scope, while more painful initially, would have completely removed all of these issues.</p>
<p>Right now I have even resorted to &#8220;compiling&#8221; plugins &#8212; parsing the files before they are executed, grepping all the &#8220;global xxx&#8221; definitions, and proactively making them global before they are defined. Huge pain, but necessary if I want wordpress to run at specific points in a third-party application &#8212; e.g. during user authentication.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe Cascio</title>
		<link>http://mgeisler.net/2005/05/global-variables-in-wordpress/#comment-316353</link>
		<dc:creator>Joe Cascio</dc:creator>
		<pubDate>Sat, 06 Dec 2008 11:38:45 +0000</pubDate>
		<guid isPermaLink="false">http://mgeisler.net/2005/05/24/global-variables-in-wordpress/#comment-316353</guid>
		<description>&lt;p&gt;I have to agree with Martin. I just got into WordPress development, and frankly, I was shocked to see such a bad practice used in what is clearly one of the most frequently used APIs in web-development. Use of global variables really cannot be defended in any way, shape or form. It causes nearly invisible coupling between otherwise completely unrelated pieces of code. Pandering to the ignorance of non-programmers and newbies is no excuse. It only makes the code even more difficult to maintain or modify for them later on.&lt;/p&gt;

&lt;p&gt;During my tenure as Chief Technology Officer at a medical imaging software company, I let the programming staff know that using a global variable was cause for termination. That's how seriously bad a practice it is.&lt;/p&gt;

&lt;p&gt;I won't go into the details, but any global can be replaced by either passing a variable into a function, or in the case of a true singleton, a global function. Unfortunately, this will probably never happen because there is now so much code out there that relies on existing ones, which, by the way, is one of the real problems with globals. Once you start to use them, it's very work intensive to get them back out. I'm surprised there hasn't been more outrage from educated professionals on this subject.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I have to agree with Martin. I just got into WordPress development, and frankly, I was shocked to see such a bad practice used in what is clearly one of the most frequently used APIs in web-development. Use of global variables really cannot be defended in any way, shape or form. It causes nearly invisible coupling between otherwise completely unrelated pieces of code. Pandering to the ignorance of non-programmers and newbies is no excuse. It only makes the code even more difficult to maintain or modify for them later on.</p>
<p>During my tenure as Chief Technology Officer at a medical imaging software company, I let the programming staff know that using a global variable was cause for termination. That&#8217;s how seriously bad a practice it is.</p>
<p>I won&#8217;t go into the details, but any global can be replaced by either passing a variable into a function, or in the case of a true singleton, a global function. Unfortunately, this will probably never happen because there is now so much code out there that relies on existing ones, which, by the way, is one of the real problems with globals. Once you start to use them, it&#8217;s very work intensive to get them back out. I&#8217;m surprised there hasn&#8217;t been more outrage from educated professionals on this subject.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Idetrorce</title>
		<link>http://mgeisler.net/2005/05/global-variables-in-wordpress/#comment-140112</link>
		<dc:creator>Idetrorce</dc:creator>
		<pubDate>Sat, 15 Dec 2007 13:00:14 +0000</pubDate>
		<guid isPermaLink="false">http://mgeisler.net/2005/05/24/global-variables-in-wordpress/#comment-140112</guid>
		<description>&lt;p&gt;very interesting, but I don't agree with you 
Idetrorce&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>very interesting, but I don&#8217;t agree with you<br />
Idetrorce</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Martin Geisler</title>
		<link>http://mgeisler.net/2005/05/global-variables-in-wordpress/#comment-856</link>
		<dc:creator>Martin Geisler</dc:creator>
		<pubDate>Wed, 01 Feb 2006 19:25:04 +0000</pubDate>
		<guid isPermaLink="false">http://mgeisler.net/2005/05/24/global-variables-in-wordpress/#comment-856</guid>
		<description>&lt;p&gt;Thanks for your comments, I'm glad to hear that I'm not all offtrack in my critique :-)&lt;/p&gt;

&lt;p&gt;And about WordPress 2: no need to search for my comments on it  --- I haven't bothered to upgrade to WP 2 yet.  And from what I've read about it around the net I'll wait a bit more... (also because I have little time right now).&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Thanks for your comments, I&#8217;m glad to hear that I&#8217;m not all offtrack in my critique :-)</p>
<p>And about WordPress 2: no need to search for my comments on it  &#8212; I haven&#8217;t bothered to upgrade to WP 2 yet.  And from what I&#8217;ve read about it around the net I&#8217;ll wait a bit more&#8230; (also because I have little time right now).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Philip</title>
		<link>http://mgeisler.net/2005/05/global-variables-in-wordpress/#comment-855</link>
		<dc:creator>Philip</dc:creator>
		<pubDate>Wed, 01 Feb 2006 19:02:57 +0000</pubDate>
		<guid isPermaLink="false">http://mgeisler.net/2005/05/24/global-variables-in-wordpress/#comment-855</guid>
		<description>&lt;p&gt;Martin,&lt;/p&gt;

&lt;p&gt;I read this entry a couple of months ago (searching for evidence why all-globals is bad), found it interesting, bookmarked it, and promptly forgot about it.&lt;/p&gt;

&lt;p&gt;Recently I started hacking around in WP a bit, and my reaction was very similar to yours.  Trying to "make things easier" for newbies by using all globals is foolhardy.   Sure, you're saving them learning a bit about OO -- but you're also training them that this is a good thing.   I recognize that blogs are meant to be more accessible/hackable to the non-technical than other apps.   But I still believe that "dumbing down" by using poor coding practices is a bad trade-off with spillover effects outside the particular project at hand.&lt;/p&gt;

&lt;p&gt;WP 2 looks marginally better in this regard, but only marginally.   I suppose I should search your blog and see if you've commented on it.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Martin,</p>
<p>I read this entry a couple of months ago (searching for evidence why all-globals is bad), found it interesting, bookmarked it, and promptly forgot about it.</p>
<p>Recently I started hacking around in WP a bit, and my reaction was very similar to yours.  Trying to &#8220;make things easier&#8221; for newbies by using all globals is foolhardy.   Sure, you&#8217;re saving them learning a bit about OO &#8212; but you&#8217;re also training them that this is a good thing.   I recognize that blogs are meant to be more accessible/hackable to the non-technical than other apps.   But I still believe that &#8220;dumbing down&#8221; by using poor coding practices is a bad trade-off with spillover effects outside the particular project at hand.</p>
<p>WP 2 looks marginally better in this regard, but only marginally.   I suppose I should search your blog and see if you&#8217;ve commented on it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Martin Geisler</title>
		<link>http://mgeisler.net/2005/05/global-variables-in-wordpress/#comment-255</link>
		<dc:creator>Martin Geisler</dc:creator>
		<pubDate>Tue, 24 May 2005 18:57:54 +0000</pubDate>
		<guid isPermaLink="false">http://mgeisler.net/2005/05/24/global-variables-in-wordpress/#comment-255</guid>
		<description>&lt;p&gt;The idea of my example was to point out that the use of global variables is dangerous because it leads to different parts of the code being connected in non-obvious ways.&lt;/p&gt;

&lt;p&gt;So whenever they are used, such connections must be prominently documented, and here --- like in almost all other places in the WordPress code where I've looked --- there were no comments in &lt;code&gt;mysql2date()&lt;/code&gt; to indicate that &lt;code&gt;locale.php&lt;/code&gt; must be loaded first.&lt;/p&gt;

&lt;p&gt;I haven't written a patch because I only just traced through the code this morning before heading to [Uni][eth], and because I'm not sure that just moving things around a little in &lt;code&gt;wp-settings.php&lt;/code&gt; would be enough --- I might mess up some other code elements which share a weak link.&lt;/p&gt;

&lt;p&gt;The choice of &lt;code&gt;mysql2date()&lt;/code&gt; was random, and I didn't expect to find such a problem at first; I expected to use the function as an example of the kind of integrity checks that must be maintained when using global variables: have they been properly initialized.  At first I thought that everything was fine, and that the variables were guaranteed to be loaded --- but then I looked at the code again and realized that it wasn't as bullit-proof as it appears.&lt;/p&gt;

&lt;p&gt;As for your last point, then I think it's pretty easy to say who is to be blamed if I don't fully understand the call order in WordPress: the developers who neglected to comment their code.  I know how to both &lt;em&gt;read&lt;/em&gt; code and how to &lt;em&gt;debug&lt;/em&gt; code --- but I much prefer to do the first.  Having comments in the code is a great way to make it readable.&lt;/p&gt;

&lt;p&gt;About the scope rules of PHP, then I believe I can say that I indeed understand them.  That's not so hard afterall --- they are &lt;a href="http://www.php.net/manual/en/language.variables.scope.php"&gt;documented&lt;/a&gt; in the manual.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>The idea of my example was to point out that the use of global variables is dangerous because it leads to different parts of the code being connected in non-obvious ways.</p>
<p>So whenever they are used, such connections must be prominently documented, and here &#8212; like in almost all other places in the WordPress code where I&#8217;ve looked &#8212; there were no comments in <code>mysql2date()</code> to indicate that <code>locale.php</code> must be loaded first.</p>
<p>I haven&#8217;t written a patch because I only just traced through the code this morning before heading to [Uni][eth], and because I&#8217;m not sure that just moving things around a little in <code>wp-settings.php</code> would be enough &#8212; I might mess up some other code elements which share a weak link.</p>
<p>The choice of <code>mysql2date()</code> was random, and I didn&#8217;t expect to find such a problem at first; I expected to use the function as an example of the kind of integrity checks that must be maintained when using global variables: have they been properly initialized.  At first I thought that everything was fine, and that the variables were guaranteed to be loaded &#8212; but then I looked at the code again and realized that it wasn&#8217;t as bullit-proof as it appears.</p>
<p>As for your last point, then I think it&#8217;s pretty easy to say who is to be blamed if I don&#8217;t fully understand the call order in WordPress: the developers who neglected to comment their code.  I know how to both <em>read</em> code and how to <em>debug</em> code &#8212; but I much prefer to do the first.  Having comments in the code is a great way to make it readable.</p>
<p>About the scope rules of PHP, then I believe I can say that I indeed understand them.  That&#8217;s not so hard afterall &#8212; they are <a href="http://www.php.net/manual/en/language.variables.scope.php">documented</a> in the manual.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Owen</title>
		<link>http://mgeisler.net/2005/05/global-variables-in-wordpress/#comment-254</link>
		<dc:creator>Owen</dc:creator>
		<pubDate>Tue, 24 May 2005 10:15:05 +0000</pubDate>
		<guid isPermaLink="false">http://mgeisler.net/2005/05/24/global-variables-in-wordpress/#comment-254</guid>
		<description>&lt;p&gt;Only really two points of concern here:&lt;/p&gt;

&lt;p&gt;In what way would you suggest that not using global variables would have fixed the problem with &lt;code&gt;mysql2date()&lt;/code&gt;?  Since you seem to have tracked down the issue to thoroughly to know the answer to this question, why haven't you written a patch for the specified bug?&lt;/p&gt;

&lt;p&gt;Your analysis of the failure of the &lt;code&gt;plugins_loaded&lt;/code&gt; bug doesn't mention that the plugin hook &lt;code&gt;init&lt;/code&gt; is a better choice for the operation you're trying to accomplish.  &lt;code&gt;plugins_loaded&lt;/code&gt; would let a plugin affect the locale settings, which would be impossible if the locale settings were loaded first.  &lt;code&gt;init&lt;/code&gt; occurs after the locale settings are set, and so manke much more sense for use in this case.  Note that the call to sink &lt;code&gt;init&lt;/code&gt; hooks is 23 lines down the file from the &lt;code&gt;plugins_loaded&lt;/code&gt; hook.&lt;/p&gt;

&lt;p&gt;So who can be blamed if you don't fully understand PHP's scope rules or WordPress' call order?&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Only really two points of concern here:</p>
<p>In what way would you suggest that not using global variables would have fixed the problem with <code>mysql2date()</code>?  Since you seem to have tracked down the issue to thoroughly to know the answer to this question, why haven&#8217;t you written a patch for the specified bug?</p>
<p>Your analysis of the failure of the <code>plugins_loaded</code> bug doesn&#8217;t mention that the plugin hook <code>init</code> is a better choice for the operation you&#8217;re trying to accomplish.  <code>plugins_loaded</code> would let a plugin affect the locale settings, which would be impossible if the locale settings were loaded first.  <code>init</code> occurs after the locale settings are set, and so manke much more sense for use in this case.  Note that the call to sink <code>init</code> hooks is 23 lines down the file from the <code>plugins_loaded</code> hook.</p>
<p>So who can be blamed if you don&#8217;t fully understand PHP&#8217;s scope rules or WordPress&#8217; call order?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
