<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>TimKadlec.com &#187; php</title>
	<atom:link href="http://timkadlec.com/tag/php-2/feed/" rel="self" type="application/rss+xml" />
	<link>http://timkadlec.com</link>
	<description>A Wisconsin based web developer writing about the web.</description>
	<lastBuildDate>Wed, 25 Jan 2012 15:39:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Quick Optimization Using Webgrind</title>
		<link>http://timkadlec.com/2010/08/quick-optimization-using-webgrind/</link>
		<comments>http://timkadlec.com/2010/08/quick-optimization-using-webgrind/#comments</comments>
		<pubDate>Tue, 24 Aug 2010 13:23:48 +0000</pubDate>
		<dc:creator>Tim Kadlec</dc:creator>
				<category><![CDATA[Performance]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://timkadlec.com/?p=664</guid>
		<description><![CDATA[I was recently working on a site whose code I was inheriting and the pages took much longer to process than I would&#8217;ve liked. Caching helped, but I wanted to get to the underlying issue so I fired up Webgrind to see if I could trace the problem. Webgrind is a freely available PHP profiling [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently working on a site whose code I was inheriting and the pages took much longer to process than I would&#8217;ve liked. Caching helped, but I wanted to get to the underlying issue so I fired up Webgrind to see if I could trace the problem.</p>
<p><a href="http://code.google.com/p/webgrind/">Webgrind</a> is a freely available PHP profiling frontend that sits on top of XDebug. Using it, you can see how many times different functions are called and find what functions called them. You can also quickly see the inclusive cost (time spent inside a function plus calls to other functions) and self cost of each function.</p>
<p>Viewing the logs for the last page load, I could see that mysql_query was called a whopping 52 times and accounted for 84.93% of the processing time (which was at an unacceptable ~3.1ms).</p>
<p>Using the Webgrind frontend, I was able to trace back 23 of those calls to one function. In turn, this function was called by one other function 17 times. I decided to focus there first.</p>
<p>Again, with the help of Webgrind, I could see that this function was called several times, in  separate files, for each page. The function produced the same results each time it was called.</p>
<p>The quick and simple fix, then, was to use a property to cache the results of that function call. So the first time it was called, it would process completely and run the necessary queries. The next time it was called, it would check it&#8217;s cache property to see if a value existed for the parameter being passed. If the value did exist &#8211; it would return that value.</p>
<p>This simple optimization immediately brought the total queries down from 52 to 36. They still accounted for 74% of the processing time, but that time had dropped dramatically from 3.1ms to ~2ms.</p>
<p>36 queries was still more than I wanted. A similar function to the one I had just optimized was responsible (both indirectly and directly) for 25 of the remaining 36 queries, so I thought I&#8217;d take a look there next.</p>
<p>Looking at the source, I could see that while the function asked for a boolean parameter to indicate if it should run certain queries, it never actually checked the value of that parameter. So no matter what, it was running all the queries, all the time. Fixing that error brought the total query count down from 36 to 16 and the total time to process the page was now ~1ms.</p>
<p>As a surprising bonus, there were no locations in the code that I had to change now that the function had been corrected. People hadn&#8217;t been expecting to get those extra values in their return object, so they never tried to use them unless they had passed a true value in for that parameter.</p>
<p>All in all I was able to take the query count down from 52 to 16 and the processing time from ~3.1ms to ~1ms. There&#8217;s more room for optimization, but this is certainly not a bad start for about 45 minutes of work.</p>
]]></content:encoded>
			<wfw:commentRss>http://timkadlec.com/2010/08/quick-optimization-using-webgrind/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Better Way To Globalize</title>
		<link>http://timkadlec.com/2008/06/a-better-way-to-globalize/</link>
		<comments>http://timkadlec.com/2008/06/a-better-way-to-globalize/#comments</comments>
		<pubDate>Wed, 18 Jun 2008 03:00:00 +0000</pubDate>
		<dc:creator>Tim Kadlec</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://localhost/1969/12/a-better-way-to-globalize/</guid>
		<description><![CDATA[Lately I've been working quite a bit with some PHP code that made heavy use of global variables. It made the code quite rigid to work with. Luckily there's a much better way: the registry pattern.]]></description>
			<content:encoded><![CDATA[<p>Lately I&#8217;ve been working quite a bit with some PHP code that made heavy use of global variables. It made the code quite rigid to work with&#8230;when changes were made in one function it had a ripple effect on many other key functions and more than once, made me curse global scope.</p>
<p>But sometimes it&#8217;s necessary to share information between different functions, so what&#8217;s a programmer to do? Global variables certainly make that possible, but they also create some problems. Heavy reliance on global variables makes it difficult to reuse code. Rarely can we uproot functions with a large dependency on global variables and insert them into different contexts or scripts without problem.</p>
<p>Debugging code also becomes difficult. When a variable is global in scope, it could be being instantiated virtually anywhere making it tough to track down. What&#8217;s even worse is coming back to that code after a year, or as it was in my case, trying for the first time to decipher code that relies on global variables.</p>
<p>Luckily there&#8217;s a much better way: the registry pattern. The registry pattern simply creates a class that servers as a central repository, or registry, of objects that we can now utilize throughout our code. It accomplishes this by utilizing static methods and properties, which means you&#8217;ll want PHP5 to accomplish it&#8230;.you can use it in PHP4 but it requires some workarounds. This pattern is particularly useful when used to store a data connection.</p>
<h2>Breaking It Down</h2>
<p>The best way to explain this is to walk through the code.</p>
<ol class="code">
<li>class Registry{</li>
<li class="tab1">private static $instance;</li>
<li class="tab1">static function instance(){</li>
<li class="tab2">if ( ! isset( self::$instance ) ){</li>
<li class="tab3">self::$instance = new self();</li>
<li class="tab2">}</li>
<li class="tab2">return self::$instance;</li>
<li class="tab1">}</li>
<li>}</li>
</ol>
<p>The code above first simply creates our registry class, aptly called Registry (line 1). We then create a static function (line 3) and variable (line 2). This is how we will keep track of whether or not an instance of this class exists. If it doesn&#8217;t, then we create a new instance of the class.</p>
<p>Now we just need to create set and get functions for anything we want to use globally. In this case, we&#8217;ll create set and get functions for a very simple object that we&#8217;ll call a TestObject and a variable to contain that object.</p>
<ol class="code">
<li>class Registry{</li>
<li>//&#8230;..</li>
<li class="tab1">private $testObject;</li>
<li class="tab1">function getTestObject(){</li>
<li class="tab2">return $this-&gt;testObject;</li>
<li class="tab1">}</li>
<li class="tab1">function setTestObject( TestObject $test){</li>
<li class="tab2">$this-&gt;testObject = $test;</li>
<li class="tab1">}</li>
<li>}</li>
</ol>
<p>Our two new methods do exactly what they sound like&#8230;the get and set methods simply retrieve or save our testObject to the $testObject variable. That&#8217;s all there is to it&#8230;now we can use our registry class to make a TestObject instance that we can use globally (for this post, just accept that TestObject allows us to set and display a message&#8230;<a href="registry.zip">all the code is available</a> for those who want a closer look). Since we are using a static property and method to ensure that all instances of that class have access to the same values, it doesn&#8217;t matter where we initially instantiate the class.</p>
<ol class="code">
<li>function setting(){</li>
<li class="tab1">$myTest = new TestObject();</li>
<li class="tab1">$myTest-&gt;setMessage ( &#8220;Registry Patterns Rock!&#8221; );</li>
<li class="tab1">$reg = Registry::instance();</li>
<li class="tab1">$reg-&gt;setTestObject( $myTest );</li>
<li>}</li>
<li>function retrieve(){</li>
<li class="tab1">$reg = Registry::instance();</li>
<li class="tab1">$myObject = $reg-&gt;getTestObject();</li>
<li class="tab1">echo ( $myObject-&gt;getMessage() );</li>
<li>}</li>
<li>setting();</li>
<li>retrieve(); //outputs Registry Patterns Rock!</li>
</ol>
<p>Now despite the fact that we create the two instances of the registry class within two separate functions, you can see that they both have access to the same values thanks to the Registry pattern, without all the mess that can be caused by global variables. To add more values to our registry class, we simply add more private variables and some set and get functions.</p>
<h2>But Wait&#8230;There&#8217;s More!</h2>
<p>Another handy way of utilizing the registry pattern, is to use our Registry instance to create and then cache an object, skipping the need for a set or get function. For example, if I have a data connection that I know I want to be standard throughout the project I can create a function like this inside of my Registry class:</p>
<ol class="code">
<li>class Registry{</li>
<li>//&#8230;..</li>
<li class="tab1">function dataConn(){</li>
<li class="tab2">if ( ! isset ( $this-&gt;dataConn ) ) {</li>
<li class="tab3">$this-&gt;dataConn = new dataConn();</li>
<li class="tab3">$this-&gt;dataConn-&gt;setHost( &#8216;localhost&#8217; );</li>
<li class="tab2">}</li>
<li class="tab2">return $this-&gt;dataConn;</li>
<li class="tab1">}</li>
<li>}</li>
</ol>
<p>Now, instead of worrying about the set and get functions, I can just call the dataConn() function. If the data connection has already been created, then it returns the connection. If not, it first creates my connection, and then returns it. So I can safely call the function without concerning myself with whether or not I&#8217;ve already created the connection&#8230;it takes care of that for me.</p>
<p>The registry pattern is so incredibly useful and a definite improvement over using the dreaded global variable in your code. I highly encourage you to play around with the <a href="registry.zip">sample code</a> and see the different ways that the registry pattern can manage your code.</p>
]]></content:encoded>
			<wfw:commentRss>http://timkadlec.com/2008/06/a-better-way-to-globalize/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

