<?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>Will Fitch&#039;s Blog &#187; LAMP</title>
	<atom:link href="http://www.willfitch.com/category/articles/lamp-tutorials/feed" rel="self" type="application/rss+xml" />
	<link>http://www.willfitch.com</link>
	<description></description>
	<lastBuildDate>Sun, 11 Dec 2011 23:39:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Optimizing Your MySQL Compilation</title>
		<link>http://www.willfitch.com/optimizing-your-mysql-compilation.html</link>
		<comments>http://www.willfitch.com/optimizing-your-mysql-compilation.html#comments</comments>
		<pubDate>Mon, 29 Nov 2010 07:05:54 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[compile]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[optimization]]></category>

		<guid isPermaLink="false">http://www.fitchpad.com/?p=57</guid>
		<description><![CDATA[System administrators constantly worry about optimization with code compilation.  This is no different for MySQL.  Most systems are fine with the binaries distributed by their flavor of Linux or directly from Oracle.  But there are many who need to maximize their hardware usage by pushing their software.  I intend to show how to tweak your MySQL compilation to achieve these results.]]></description>
			<content:encoded><![CDATA[<p>
System administrators constantly worry about optimization with code compilation.  This is no different for MySQL.  Most systems are fine with the binaries distributed by their flavor of Linux or directly from Oracle.  But there are many who need to maximize their hardware usage by pushing their software.  I intend to show how to tweak your MySQL compilation to achieve these results.
</p>
<div class="attention">
<div class="icon">This article is targeted at GCC and Linux. Some of the logic applied here can carry over to Visual C++, however.</div>
</div>
<p>
I will also be showing you how I manage MySQL upgrades and versions on my database machines.  This includes keeping the previous version of software for emergency rollback.  You never know when an unexpected bug or feature will cause problems for you, so it&#8217;s good to keep at least one historical version on hand.  I highly suggest that you let the new version of MySQL burn-in for at least 48 hours before migrating the binaries and upgrading the rest of your boxes.
</p>
<div class="note">
<div class="icon">
<p>
<strong>If you&#8217;d like to follow along in your own sandbox, you will need the following software installed and tools:</strong></p>
<ul class="bullet-2">
<li>gcc &#8211; preferably the latest version for your OS</li>
<li>gcc-c++</li>
<li>libstdc and libstdc++ development files</li>
<li>kernel development files</li>
<li>libtermcap or ncurses development files</li>
<li>glibc development files</li>
<li>make</li>
<li>wget &#8211; for downloading MySQL</li>
<li>SSH or console access to your Linux machine</li>
<li>Some patience while reading and understanding this tutorial!</li>
</ul>
<p>Depending on your OS, you may need additional tools that are normally included with standard distributions.
</p>
</div>
</div>
<div class="contentheading">An Overview of Version Maintenance</div>
<p>
There have been cases where a version of MySQL that I&#8217;ve compiled has introduced problems.  The majority of the time this is related to replication or collations.  Whatever the case, I had the need to rollback to a version that was working fine.  You can typically do this for minor upgrades (e.g. 5.1.51 to 5.1.50) with a relative confidence.  That level of certainty does not apply to major and major-minor (5.0.x to 5.1.x or 4.x to 5.x).
</p>
<p>
In order to accomplish this, I keep all versions of MySQL in the same directory with at least one previous version.  My versions are installed in /opt.  The installation directory is always named mysql-[version].  For example, if I&#8217;m installing MySQL 5.1.52, the path to the installation will be /opt/mysql-5.1.52.  This let&#8217;s me keep a lot of historical versions on hand while also installing and distributing new versions without stopping MySQL.  The production version is a symbolic link to the full path of the installation.  For example, if version 5.1.51 is the production version, my symbolic link would be created like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>mysql-5.1.51 <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>mysql</pre></div></div>

<p>A listing of /opt would yield something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">drwxr-xr-x  <span style="color: #000000;">4</span> root root <span style="color: #000000;">4096</span> Sep <span style="color: #000000;">20</span> <span style="color: #000000;">20</span>:<span style="color: #000000;">52</span> .<span style="color: #000000; font-weight: bold;">/</span>
drwxr-xr-x <span style="color: #000000;">25</span> root root <span style="color: #000000;">4096</span> Sep <span style="color: #000000;">23</span> <span style="color: #000000;">23</span>:<span style="color: #000000;">42</span> ..<span style="color: #000000; font-weight: bold;">/</span>
lrwxrwxrwx  <span style="color: #000000;">1</span> root root   <span style="color: #000000;">17</span> Sep <span style="color: #000000;">20</span> <span style="color: #000000;">20</span>:<span style="color: #000000;">36</span> mysql -<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>mysql-5.1.51<span style="color: #000000; font-weight: bold;">/</span>
drwxrwxr-x  <span style="color: #000000;">9</span> root root <span style="color: #000000;">4096</span> Jul <span style="color: #000000;">23</span> <span style="color: #000000;">21</span>:<span style="color: #000000;">40</span> mysql-5.1.50<span style="color: #000000; font-weight: bold;">/</span>
drwxrwxr-x  <span style="color: #000000;">9</span> root root <span style="color: #000000;">4096</span> Sep <span style="color: #000000;">20</span> <span style="color: #000000;">20</span>:<span style="color: #000000;">34</span> mysql-5.1.51<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>Notice that the previous version, 5.1.50, still exists in the event I have to rollback.  If this happens, I simply take the following actions:</p>
<ul class="bullet-6">
<li>Stop the running MySQL instance</li>
<li>Destroy the existing symbolic link</li>
<li>Create a new symbolic link that points to the previous version</li>
<li>Start MySQL via /opt/mysql/bin/mysqld_safe</li>
<li>Verify the problem no longer exists</li>
</ul>
<p>
The same logic applies to upgrading, but rather than creating a symbolic link to the previous version, it gets pointed to the new.
</p>
<div class="contentheading">Preparing the Compilation</div>
<p>
Prior to actually configuring, compiling and installing the new version of MySQL, we need to set some flags for the C and C++ compilers.  This is where much of the optimization will come into play.  Keep in mind that this is not all-inclusive, and <a href="http://gcc.gnu.org/onlinedocs/">there&#8217;s likely more compiler optimizations you can make for your distribution and hardware</a>.
</p>
<h5>CPU-specific Flags</h5>
<p>
Depending on whether you&#8217;re using Intel or AMD processors, you will have some different flags to set.  In my case, I&#8217;m using dual Xeon 5460s.  The flag I use is SSE4.1, which is labeled for Core i7, but is generically used for X5400/5500 CPUs as well.</p>
<p>If you&#8217;re using AMD, you will have a different list to choose from.  The full list of CPU flags can be found <a href="http://gcc.gnu.org/onlinedocs/gcc/i386-and-x86_002d64-Options.html">here</a>.  The generic, x86_64 architecture flag for AMD is athlon64.
</p>
<p>
Passing these flags to the compiler is done with two Linux environmental variables: CFLAGS and CXXFLAGS.  CFLAGS is for the C compiler while CXXFLAGS is for the C++.
</p>
<p>
Now that we have the CPU-specific flags worked out, let&#8217;s discuss additional optimization flags.  One of the most important flags you&#8217;ll set is the optimization flag (-O).  There are technically five levels of optimization, but I&#8217;ll discuss only three: -O1, O2 and O3.  Each of these levels represents GCC&#8217;s attempt to optimize the code produced from the source files. I&#8217;ll briefly explain these:</p>
<ul class="bullet-7">
<li>O1 &#8211; Level one optimizes common features that don&#8217;t require speed-space tradeoffs.  The executable produced by this level is usually faster and smaller in size than with no optimization.</li>
<li>O2 &#8211; Level two includes level one optimizations and many, many others.  Generally speaking, this is the route to go with on most distributions.</li>
<li>O3 &#8211; This includes all optimization possibilities.  Be careful using this one as is produces a rather large executable. While in some cases this may produce faster code, it has had the opposite affect with me.  The only way to truly know if you should choose level two or three is trial and error.  Compile the same version of MySQL with both flags and test which is faster.</li>
</ul>
<p>The other two are O0 or no optimization and Os optimizes to produce the smallest possible executable for systems constrained by memory and disk.
</p>
<p>
For a full list of features that are optimized by level, see <a href="http://www.linuxjournal.com/files/linuxjournal.com/linuxjournal/articles/072/7269/7269t1.jpg">this image</a>.
</p>
<p>
The next flag is &quot;pipe&quot;.  Pipe directs GCC to take piped input and use RAM whenever possible rather than disk.  In most cases, you won&#8217;t see a big performance gain from this, but we&#8217;re trying to achieve maximum results.  Next we look at C++ flags.
</p>
<p>
There are two additional flags passed to the C++ compiler in conjunction with those mentioned above: no-exceptions and no-rtti.
</p>
<p>
The flag no-exceptions removes error handling as it relates to exceptions in the produced C++ code.  MySQL actually recommends this flag in order to increase the stability of the produced executable.
</p>
<div class="alert">
<div class="icon">If you use try/catch or throw, you <strong>should not use</strong> -fno-exceptions.</div>
</div>
<p>
Finally we come to no-rtti.  The answer to this is simple: MySQL doesn&#8217;t use RTTI, so the result is a smaller and potentially faster executable.
</p>
<p>
Our CFLAGS and CXXFLAGS declaration will look like this for Intel:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">CFLAGS</span>=<span style="color: #ff0000;">&quot;-O2 -msse4 -pipe&quot;</span> <span style="color: #007800;">CXXFLAGS</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${CFLAGS}</span> -fno-exceptions -fno-rtti&quot;</span></pre></div></div>

<p>and this for generic AMD:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">CFLAGS</span>=<span style="color: #ff0000;">&quot;-O2 --march=athlon64 -pipe&quot;</span> <span style="color: #007800;">CXXFLAGS</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${CFLAGS}</span> -fno-exceptions -fno-rtti&quot;</span></pre></div></div>

<p>Go ahead and execute that in your shell, setting the environmental variables.  If you&#8217;d like to test that they&#8217;re valid, issue the following commands:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$CFLAGS</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$CXXFLAGS</span></pre></div></div>

</p>
<h5>Executing the Configure Script</h5>
<p>
The configure scripts works to produce a valid makefile.  The makefile contains the instructions to compile, install and clean the MySQL distribution you&#8217;re working with.  There are some important options to pass to this script that will produce a faster binary.  Not all fall under this category, and I&#8217;ll discuss each option shown in the example below:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>configure \
  <span style="color: #660033;">--prefix</span>=<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>mysql-5.1.52 \
  <span style="color: #660033;">--enable-static</span> \
  <span style="color: #660033;">--with-charset</span>=utf8 \
  <span style="color: #660033;">--enable-thread-safe-client</span> \
  <span style="color: #660033;">--without-debug</span> \
  <span style="color: #660033;">--with-fast-mutexes</span> \
  <span style="color: #660033;">--enable-assembler</span> \
  <span style="color: #660033;">--with-plugins</span>=innobase,partition</pre></div></div>

</p>
<p>
Prefix (<span class="highlight">&#8211;prefix</span>) creates a variable that prefixes all installation directives.  In the example above, we use /opt/mysql-5.1.52.  This means that all executables, man pages, directories and scripts will be placed inside that specified directory.  </p>
<p>Enable static (<span class="highlight">&#8211;enable-static</span>) enables static linking to components. There is a trade-off with this.  While it produces a faster executable, the footprint of the file is much larger.  We will be able to dramatically reduce that later, however.</p>
<p>Setting the character set to UTF-8 (<span class="highlight">&#8211;with-charset=utf8</span>) allows us to support multiple languages and characters outside of the default Latin character set.  This is optional and by far not an optimization technique.  It could easily be argued that this has the opposite affect, but if you plan to support a global audience, I&#8217;d suggest choosing this option.</p>
<p>Enabling a thread-safe client (<span class="highlight">&#8211;enable-thread-safe-client</span>) produces the client software such as mysql, mysqladmin, mysqldump, etc. with thread-safe capabilities.</p>
<p>Disabling debugging (<span class="highlight">&#8211;without-debug</span>) is an obvious performance booster.  We most definitely don&#8217;t want MySQL to be worrying about providing that kind of information on a production system.</p>
<p>Fast mutexes (<span class="highlight">&#8211;enable-fast-mutexes</span>) is another one of those trial and error situations.  Depending on your machine, fast mutexes may or may not provide benefit.  As with optimization level three, you will have to experiment to determine whether you should use this option.</p>
<p>Using an assembler (<span class="highlight">&#8211;enable-assembler</span>) tells MySQL to use those versions of some string functions. This provides some optimization but not a lot.</p>
<p>You need to determine which plugins (<span class="highlight">&#8211;with-plugins</span>) to use with your distribution.  This is an application-level decision and the less you use the better off you are.  InnoDB is the premier transactional engine for MySQL, so I&#8217;d highly suggest enabling and using it.  The partition plugin allows you to scale your tables based on your defined criteria.
</p>
<div class="alert">
<div class="icon">Be careful with partitioning! Most use this functionality to overcome file system limitations, but you can bog down your queries if you aren&#8217;t careful to choose the best partition algorithm.
</div>
</div>
<div class="contentheading">Compiling and Installing MySQL</div>
<p>
Now that we&#8217;ve prepped our software, it&#8217;s time to compile and install! This is my favorite part.  I truly enjoy watching the makefile instruct GCC to chunk the source code and produce the fastest possible software.  This makes me a super-dork, but it is what it is.
</p>
<p>
Execute the following command in your shell:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">make</span></pre></div></div>

<p>This starts the compilation of the software. Get up, stretch your legs and grab some coffee.  This is going to take a while.
</p>
<p>
Once the compilation is complete, we&#8217;re ready to install the software.  This is an easy one:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>List your /opt directory to ensure the software is good to go:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-al</span> <span style="color: #000000; font-weight: bold;">/</span>opt</pre></div></div>

</p>
<div class="highlight1">
<h3>Strip That Code!</h3>
<p>Before testing and switching to the new version of MySQL, we need to make one last optimization.  Since we&#8217;ve compiled the code statically, we have a ton of junk symbols we can remove.  This turns a 30 MB file into a 3MB file and increases our executable&#8217;s startup speed up to 80%!
</p></div>
<p>
<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">strip</span> <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>mysql-5.1.52<span style="color: #000000; font-weight: bold;">/</span>libexec<span style="color: #000000; font-weight: bold;">/</span>mysqld</pre></div></div>

<p>You can also execute this on any binary file within your new distribution, but mysqld is certainly the most important.
</p>
<h5>Activating the New MySQL Version</h5>
<p>
Activating the new MySQL version is easy:</p>
<ul class="bullet-6">
<li>
Stop the running MySQL instance:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mysqladmin shutdown</pre></div></div>

</li>
<li>
Destroy the existing symbolic link:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>mysql</pre></div></div>

</li>
<li>
Create a new symbolic link that points to the new version:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>mysql-5.1.52 <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>mysql</pre></div></div>

</li>
<li>
Start MySQL via /opt/mysql/bin/mysqld_safe:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mysqld_safe <span style="color: #660033;">--user</span>=mysql <span style="color: #000000; font-weight: bold;">&amp;</span></pre></div></div>

</li>
</ul>
<p>There are additional optimization techniques you take use when configuring your MySQL instance (such as CPU priority), but I&#8217;ll save that for another entry.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/optimizing-your-mysql-compilation.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using ActiveRecord in Fever Framework</title>
		<link>http://www.willfitch.com/using-activerecord-in-fever-framework.html</link>
		<comments>http://www.willfitch.com/using-activerecord-in-fever-framework.html#comments</comments>
		<pubDate>Tue, 16 Feb 2010 13:46:29 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[lamp]]></category>

		<guid isPermaLink="false">http://www.willfitch.com/?p=234</guid>
		<description><![CDATA[One of the features of the PHP Fever Framework is ActiveRecord access to your database. This is meant to be a database access tool for DB models and contains many of the ActiveRecord functions and more.]]></description>
			<content:encoded><![CDATA[<p>One of the features of the PHP Fever Framework is ActiveRecord access to your database. This is meant to be a database access tool for DB models and contains many of the <a href="http://en.wikipedia.org/wiki/Active_record_pattern">ActiveRecord</a> functions and more.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/using-activerecord-in-fever-framework.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Chain-of-Command Pattern: OOP Techniques in PHP</title>
		<link>http://www.willfitch.com/the-chain-of-command-pattern-oop-techniques-in-php.html</link>
		<comments>http://www.willfitch.com/the-chain-of-command-pattern-oop-techniques-in-php.html#comments</comments>
		<pubDate>Tue, 10 Mar 2009 12:07:43 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[chain of command]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php5]]></category>

		<guid isPermaLink="false">http://www.phpfever.com/?p=160</guid>
		<description><![CDATA[The chain-of-command pattern, like most others, assists with maintaining a loose coupling within your classes.  By providing a series of classes that implement the ICommand interface and do a specific bit of processing, the developer doesn't have to care which method to execute.]]></description>
			<content:encoded><![CDATA[<p>The chain-of-command pattern, like most others, assists with maintaining a loose coupling within your classes.  By providing a series of classes that implement the ICommand interface and do a specific bit of processing, the developer doesn&#8217;t have to care which method to execute.</p>
<p><strong>The ICommand Interface</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> ICommand
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * Run a command
     * @param string $command_type
     * @param args $args
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onCommandCall<span style="color: #009900;">&#40;</span><span style="color: #000088;">$command_type</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This simple interface provides a method for executing a call to initiate a bit of functionality. Along with the classes that execute the functionality, you&#8217;ll need a class to handle the chain.  We&#8217;ll provide this class with another interface requiring the use of a method to add chains and execute commands:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> ICommandBuilder
<span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * Run a command
	 *
	 * @param string $command_type
	 * @param mixed $args
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> runCommand<span style="color: #009900;">&#40;</span><span style="color: #000088;">$command_type</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Add a new command class
	 *
	 * @param mixed $commanding_class
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addCommand<span style="color: #009900;">&#40;</span><span style="color: #000088;">$commanding_class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The two methods here are used for executing commands and adding new command classes.  Let&#8217;s build the chain-of-command class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> ChainBuilder implements ICommandBuilder
<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_commands</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> runCommand<span style="color: #009900;">&#40;</span><span style="color: #000088;">$command_type</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_commands<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_commands <span style="color: #b1b100;">as</span> <span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">onCommandCall</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$command_type</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addCommand<span style="color: #009900;">&#40;</span><span style="color: #000088;">$commanding_class</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_commands<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$commanding_class</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The class above provides a mechanism for chaining your requests.  You add the necessary commands to execute, then pass requests to runCommand, which will loop through all of the commands until one successfully completes the action, or it exhausts all commands.  Here you could add interface checks and throw the necessary exceptions for commands not found, etc.</p>
<p>Let&#8217;s add a couple of command implementations: email and stream.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> EmailCommand implements ICommand
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onCommandCall<span style="color: #009900;">&#40;</span><span style="color: #000088;">$command</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$command</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// send an email with $args</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> StreamCommand implements ICommand
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onCommandCall<span style="color: #009900;">&#40;</span><span style="color: #000088;">$command</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$command</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'writeStream'</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// Write the stream</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>These commands email a user and write to a stream.  Now let&#8217;s use them!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$command</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ChainBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$command</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addCommand</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> StreamCommand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addCommand</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> EmailCommand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$command</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">runCommand</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'me@you.com'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$command</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">runCommand</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'writeStream'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Hi, stream!'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Conclusion</strong></p>
<p>At first glance, the chain-of-command pattern may look like overhead.  But on a large code base, this code be useful when making a lot of modifications, etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/the-chain-of-command-pattern-oop-techniques-in-php.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Sync Your Linux Box to Ensure an Accurate Clock</title>
		<link>http://www.willfitch.com/sync-your-linux-box-to-ensure-an-accurate-clock.html</link>
		<comments>http://www.willfitch.com/sync-your-linux-box-to-ensure-an-accurate-clock.html#comments</comments>
		<pubDate>Thu, 26 Jul 2007 20:32:45 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://www.phpfever.com/sync-your-linux-box-to-ensure-an-accurate-clock.html</guid>
		<description><![CDATA[This is a simple tip to keep your Linux box&#8217;s clock up-to-date (quite literally). Using your ntpdate program, set a cron for say 30 minutes to sync your time with a NTP server. 30 * * * * /usr/sbin/ntpdate -s ntp-2.mcs.anl.gov The above server is available to US machines, and requires an email notification for [...]]]></description>
			<content:encoded><![CDATA[<p>This is a simple tip to keep your Linux box&#8217;s clock up-to-date (quite literally).  Using your ntpdate program, set a cron for say 30 minutes to sync your time with a NTP server.</p>

<div class="wp_syntax"><div class="code"><pre class="ssh" style="font-family:monospace;">30 * * * * /usr/sbin/ntpdate -s ntp-2.mcs.anl.gov</pre></div></div>

<p>The above server is available to US machines, and requires an email notification for usage as do most.  <a href="http://www.eecis.udel.edu/~mills/ntp/clock2b.html" title="Complete list of NTP servers">Click here for a more complete list of NTP servers.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/sync-your-linux-box-to-ensure-an-accurate-clock.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

