<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Will Fitch's Blog</title>
	
	<link>http://www.willfitch.com</link>
	<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.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/willfitch" /><feedburner:info uri="willfitch" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:browserFriendly></feedburner:browserFriendly><item>
		<title>PHP Typehint Return Values</title>
		<link>http://www.willfitch.com/php-typehint-return-values.html</link>
		<comments>http://www.willfitch.com/php-typehint-return-values.html#comments</comments>
		<pubDate>Sun, 11 Dec 2011 04:59:02 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.willfitch.com/?p=322</guid>
		<description><![CDATA[As some of you know, I&#8217;ve been working for a while on implementing type hinting for method return values. I&#8217;m pleased to say that the implementation is done. The following types are allowed: Array Class names Parameter type hints allow callable types, but until a keyword is assigned to &#8220;callable&#8221; (RFC), it&#8217;s not possible to [...]]]></description>
			<content:encoded><![CDATA[<p>As some of you know, I&#8217;ve been working for a while on implementing type hinting for method return values.  I&#8217;m pleased to say that the implementation is done.  </p>
<p>The following types are allowed:</p>
<ul>
<li>Array</li>
<li>Class names</li>
</ul>
<p>Parameter type hints allow callable types, but until a keyword is assigned to &#8220;callable&#8221; <a href="https://wiki.php.net/rfc/callable">(RFC)</a>, it&#8217;s not possible to implement this for method returns.  To be honest, I&#8217;m not completely sold that it <strong>should</strong> be implemented at all.  That&#8217;s another conversation.</p>
<p>The syntax is simple &#8211; rather than declaring &#8220;function&#8221;, you replace it with array or your class name.  Since mixed types are allowed, you can still use &#8220;function&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> ArrayIterator getIterator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayIterator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #990000;">array</span> getArray<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'I am returning an array'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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> thisStillWorks<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'This still works, too.'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The same rules and errors for type hint parameters apply if you attempt to return a value not specified by the return type; a catchable fatal error will be thrown.  Note that this is <strong>NOT</strong> an exception.</p>
<p><strong>Reflection</strong></p>
<p>I added &#8220;getReturnType()&#8221; to the ReflectionMethod class.  It will return one of these three values: &#8220;mixed&#8221; &#8211; for function, &#8220;array&#8221; or &#8220;ClassName&#8221; if an object is specified.</p>
<p><strong>Interfaces</strong></p>
<p><strike>Interface implementation is incomplete.  I will be working on that this week. That said, if you specify an interface on the return type, it works just fine.  The only thing not implemented is validation on overloading interface methods.</strike></p>
<p>Interfaces and type hinting return values are fully implemented now.  If, for example, you implement an interface which defines a method that returns an array, and you attempt to change it, an E_COMPILE_ERROR is raised.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">interface</span> MyInterface
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> blah<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> YourClass implements MyInterface
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> blah<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'works!'</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> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> MyInterface getValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> YourClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The above works and validates correctly.</p>
<p><strong>Help</strong></p>
<p>If you&#8217;re interested in testing out this patch, shoot me an email using the <a href="http://www.willfitch.com/about">contact page</a>.  If you&#8217;d just like to play with the functionality, go to <a href="https://github.com/fitchwh" title="my GitHub page">my GutHub page</a> and <a href="http://github.com/fitchwh/php-src">download</a> or <a href="git@github.com:fitchwh/php-src.git">clone</a> the repo.  The branch I&#8217;m working out of is &#8220;returntype&#8221;.  Just make sure you compile with &#8211;enable-debug.</p>
<p>If you happen to run into any memory leaks, please provide any information returned from the output.</p>
<p>Until next time. <img src='http://www.willfitch.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/php-typehint-return-values.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Distributed Database Systems</title>
		<link>http://www.willfitch.com/distributed-database-systems.html</link>
		<comments>http://www.willfitch.com/distributed-database-systems.html#comments</comments>
		<pubDate>Fri, 03 Dec 2010 01:21:13 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Distributed Systems]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Operations]]></category>
		<category><![CDATA[distributed systems]]></category>

		<guid isPermaLink="false">http://www.willfitch.com/?p=292</guid>
		<description><![CDATA[In today's web-enabled world, the use of Database Management Systems (DBMS) is extremely common and becoming even more necessary as each day passes.  In most cases, the choice is your typical RDBMS such as MySQL, PostreSQL, SQLite or SQL Server.  Some larger, enterprise-level systems choose larger solutions such as Oracle.  

As a company grows, it becomes a much bigger pain and seemingly less logical to maintain data relationally.  It requires more and bigger hardware, labor, decreased performance and becomes difficult to maintain and scale out.  This can partly be blamed for the nature of relational systems. ]]></description>
			<content:encoded><![CDATA[<p>In today&#8217;s web-enabled world, the use of Database Management Systems (DBMS) is extremely common and becoming even more necessary as each day passes.  In most cases, the choice is your typical RDBMS such as MySQL, PostreSQL, SQLite or SQL Server.  Some enterprise-level systems choose larger solutions such as Oracle.  </p>
<p>As a company grows, it becomes a much bigger pain and seemingly less logical to maintain data in this manner.  It requires more and bigger hardware, labor, decreases performance and becomes difficult to maintain and scale out.  This can partly be blamed for the nature of relational systems. </p>
<div class="contentheading">Traditional Relational Database Management Systems</div>
<p>Most RDBMS include the following high-level functions: querying, retrieving/writing data and replication.  Each node acts on its own with identical or nearly identical copies of the data and, depending on whether the server is a slave, master or both, requires execution of the same query across the network in order to maintain the data.  The query execution may come directly from the client or via replication from a master.  </p>
<p>This is a relatively quick and easy start-up solution that requires less work as compared to a distributed system.  The typical master-slave setup can, for the most part, be automated for new hardware.  Just purchase similar hardware, increment the slave&#8217;s connection identifier, copy the data from another slave or snapshot and start it up.</p>
<p>To an extent, it&#8217;s also easy to scale.  If you&#8217;re currently able to serve 2,000 queries per second, and your business grows to need 2,500, you simply add a new server.  This is sufficient for many &#8211; probably most companies.  But how about the need for 100,000, 300,000 or 2,000,000 queries per second?  This is where the dwindling begins.</p>
<p>My rule of thumb is one master per four slaves at most &#8211; providing me with a 25% write-to-read ratio.  Unfortunately, this also means I need some hefty, expensive hardware to do the job.  A typical master for us at <a href="http://www.quepasa.com">quepasa.com</a> is dual, quad-core Xeons with 72GB of RAM.  Since we are a heavy write-based system, we need fast disks; we also have a lot of data.  In order to scale a solution like this, we have SANs in both data centers.  Again, also very expensive.  It works like a charm for now, but I&#8217;m constantly worried about what happens when we have 300,000 or more new users a day signing up.  At what point does expense and maintenance start outweighing the benefit of this bulky hardware setup?  </p>
<div class="contentheading">Distributed Database Systems</div>
<p>A few weeks ago I started research how DDMSs could work at our company.  There were and still are many questions that I have to answer in this regard. What advantages and disadvantages will come with this new architecture?  What hardware can I reuse efficiently with this new setup?  What vendor do I choose to go with?  What kind of code changes and culture shock will this introduce to the developers and DBAs?</p>
<h5>Advantages</h5>
<p>To begin with, this solution is extremely scalable.  The general concept of a distributed database means expansion will be easier while not sacrificing reliability or availability.  If I need to add more hardware, I should be able to pause a node or partition, copy it over and start the new hardware up (at a very high-level) without worrying about the rest of the system taking a massive beating and risking a crash or series of crashes.  </p>
<p>Since this setup doesn&#8217;t require those massive machines we&#8217;re using right now, we can use commodity hardware that provides decent RAM (most of the expense of those bigger machines is here) and good CPUs.  Off the top of my head, I imagine the hardware I&#8217;d consider for this would have four to eight GB of RAM and dual, quad-core Core i7s or possibly Xeons of the Westmere flavor.  I definitely want to provide as many cores as I can to each node for higher throughput.  This scenario likely means the bottleneck will now become the network itself.  These nodes need to be able to communicate often, so a 10GbE network would eventually need to replace the 1 GbE.  Speaking of which, I&#8217;ll move on to disadvantages.</p>
<h5>Disadvantages</h5>
<p>For our company, and probably all others in a similar scenario, the biggest disadvantage would be code and culture change.  We&#8217;re used to writing code that connects to a database and executes a stored procedure that lives in the database and is written in <a href="http://en.wikipedia.org/wiki/SQL">SQL</a>.  Introducing this new architecture would completely change our environment.  Stored procedures would likely be written in <a href="http://en.wikipedia.org/wiki/Java_%28programming_language%29">Java</a> or another <a href="http://en.wikipedia.org/wiki/Just-in-time_compilation">JIT</a> language.  The <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</a> functionality would then execute that instead.  This essentially means a rewrite of much of the site.  </p>
<p>Building on that, we also introduce additional complexities into the architecture.  This potentially means more cost in labor.  Though the amount of time I put into weekend and late night/early morning maintenance renders that point moot.</p>
<p>We&#8217;ve also invested a lot of time and money into the hardware and architecture we have today.  Introducing the concept of commodity hardware into the system could cause both economical and political backlashes.  Fortunately, this likely appears more extreme than it really is.  That bulky hardware could easily be sold at a decent price and the money from that used to purchase enough commodity hardware and then some.  I&#8217;m not as worried about this aspect.</p>
<p>There are also solution-specific problems that are introduced. The concept of DDBMS itself is rather young and the lack of options and experience raises concern.  This also means there&#8217;s a lack of standards to follow meaning moving to other solutions would be a major PIA at best.  I would also be concerned with how the vendor treats concurrency control among the nodes.  This is a major consideration in this type of environment.</p>
<div class="contentheading">Options</div>
<p>While there are a few distributed solutions out there: <a href="http://hadoop.apache.org/">Hadoop</a>, <a href="http://incubator.apache.org/cassandra/">Cassandra</a>, <a href="http://hypertable.org/">Hypertable</a>, Amazon <a href="http://aws.amazon.com/simpledb/">SimpleDB</a>, <a href="http://nosql-database.org/">etc.</a>, one stands out in my opinion &#8211; <a href="http://voltdb.com/">VoltDB</a>.</p>
<p><a href="http://voltdb.com/">VoltDB</a> is a next-generation SQL RDBMS with ACID for fast-scaling OLTP applications.  It&#8217;s core is written in C++ and they provide many client connectors with Java the forefront. After looking into and reading more about them, I decided to query John Hugg (<a href="http://twitter.com/johnhugg">@johnhugg</a>) for the potential of a PHP client.  To my surprise, they already had one.  John pointed me to the repository for the extension and I had a look at it.  While John did an excellent job with the extension, it requires the use of a <a href="http://www.swig.org/">SWIG</a> and it&#8217;s written in C++.  </p>
<p>I asked John what he thought about a wrapper written in C and a pure C extension for PHP that is compiled against that library.  It feels like a true &#8220;PHP solution&#8221; that could be added to <a href="http://pecl.php.net">PECL</a> at some point and would eliminate the need for a SWIG.  He was very excited about the potential for a more native solution.  </p>
<p>That brings us to today.  At this point, we&#8217;ve decided to research more into the potential for a C API.  And once I get a better grasp on the core of VoltDB, I&#8217;ll be able to help John, et al. with a roadmap.  The PHP extension will come after that.  </p>
<div class="contentheading">Conclusion and Thoughts</div>
<p>I&#8217;m extremely excited about VoltDB and what opportunities it will bring to <a href="http://www.quepasa.com">quepasa.com</a>.  I feel comfortable with the concept but skittish with the PHP integration.  I&#8217;m confident that over the next year we&#8217;ll be able to come up with a good outcome and hopefully help others find their scaling solution in VoltDB.</p>
<p>If you&#8217;re interested in learning more about or helping with the C API and PHP extension efforts, visit the <a href="http://community.voltdb.com/">VoltDB community page</a> and signup for the lists or leave a comment here.  You can also <a href="http://community.voltdb.com/downloads">download Volt</a> and play with it in your own sandbox.  It&#8217;s open source, so you can check out the core as well.</p>
<div class="highlight1">
<p>
<span class="highlight"><strong>Update</strong></span>
</p>
<p>
After reading <a href="http://nosql.mypopescu.com/post/2083218626/distributed-database-systems">Alex Popescu&#8217;s post</a>, I realized how bias my post sounded.  For clarification purposes, I have not reached a final decision.  In fact, I&#8217;ve chosen VoltDB as a starting point based on internal talents and familiarity.  If VoltDB turns out to not be for my transition, I won&#8217;t use it.  I do want the best technology but also have to keep things realistic.
</p>
<p>
This, however, does not stop the fact that I will be assisting VoltDB in their efforts with the C API and PHP extension.  I like what they do and will continue to support them from an open source perspective.  Your comments are welcome!
</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/distributed-database-systems.html/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<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>Fever Framework Update</title>
		<link>http://www.willfitch.com/fever-framework-update.html</link>
		<comments>http://www.willfitch.com/fever-framework-update.html#comments</comments>
		<pubDate>Thu, 11 Feb 2010 16:06:35 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[PHP Fever Framework]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.willfitch.com/?p=224</guid>
		<description><![CDATA[I just want to update everyone on the status of the framework. It is still on schedule for the February 26th release! As we countdown to the public beta release, I want to keep you informed of some exciting functionality that&#8217;s being added. Functionality Along with the JSON controller, a new XML controller has been [...]]]></description>
			<content:encoded><![CDATA[<p>I just want to update everyone on the status of the framework. It is still on schedule for the February 26th release!</p>
<p>As we countdown to the public beta release, I want to keep you informed of some exciting functionality that&#8217;s being added.</p>
<p><strong>Functionality</strong></p>
<p>Along with the JSON controller, a new XML controller has been added. This allows you to return nearly anything from an action and it will automatically convert it to XML, send the proper content-type header and provide the data to the client.  Here is an example of how it would work:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyController <span style="color: #000000; font-weight: bold;">extends</span> \Fever\Controller\Action\AbstractXml
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getTestData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span>
            <span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Will Fitch'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'attributes'</span> <span style="color: #339933;">=&gt;</span>
                <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'height'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">76</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'hair'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'brown'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'eyes'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'brown'</span>
                <span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This will, in turn, produce the following XML document to the client:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;results<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Will Fitch<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;attributes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;height<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>76<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/height<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hair<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>brown<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/hair<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;eyes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>brown<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/eyes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/attributes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/results<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Now I&#8217;m sure there are those out there that are screaming, &#8220;But I want to customize the name of the elements!!!&#8221;. No problem!  There is a method withing the AbstractXml controller called &#8220;setXmlElement&#8221; which accepts an instance of SimpleXMLElement. This will be used when converting data provided into XML.</p>
<p>This is all thanks to a new component called &#8220;XmlConverter&#8221; which lives in the \Fever\IO\Xml namespace. It follows the <a href="/the-chain-of-command-pattern-oop-techniques-in-php.html">chain-of-command</a> pattern and iterates until it finds the correct data type provided. Here are a list of converters available for iteration:</p>
<ul>
<li>ArrayToXml</li>
<li>BooleanToXml</li>
<li>NullToXml</li>
<li>NumberToXml</li>
<li>ObjectToXml</li>
<li>StringToXml</li>
</ul>
<p>All of these are separate entities, so they can be used independently.</p>
<p>I&#8217;ll continue to update throughout the upcoming days before the public beta!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/fever-framework-update.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introducing the PHP Fever Framework</title>
		<link>http://www.willfitch.com/introducing-the-php-fever-framework.html</link>
		<comments>http://www.willfitch.com/introducing-the-php-fever-framework.html#comments</comments>
		<pubDate>Fri, 05 Feb 2010 16:48:07 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[PHP Fever Framework]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.phpfever.com/?p=190</guid>
		<description><![CDATA[Overview The birth of a new MVC framework, like most others, is spawned by frustrations with the functionality of one or more other frameworks: too much emphasis on design theory, not enough in others, speed, flexibility&#8230; the list goes on. The Fever Framework is no different. In an attempt to find a happy medium with [...]]]></description>
			<content:encoded><![CDATA[<h3>Overview</h3>
<p>The birth of a new MVC framework, like most others, is spawned by frustrations with the functionality of one or more other frameworks: too much emphasis on design theory, not enough in others, speed, flexibility&#8230; the list goes on.  The Fever Framework is no different.  In an attempt to find a happy medium with design theory, speed, flexibility and latest PHP functionality, this framework is targeted at many audiences from small, shared-hosting environments to enterprise-level web applications.  That said, let me provide some goals I&#8217;ve kept in mind while developing the Fever Framework.</p>
<h3>Some Goals</h3>
<p><strong>Design theory and flexibility</strong></p>
<p>I have often wrestled among object-oriented design theory, development time and speed, not to mention optimization which is usually the 800 pound gorilla in the room.  Developers deal with this all the time; trying to squeeze a few more days in to ensure the code looks as good as it behaves.</p>
<p>The Fever Framework implements many OOP design theories and MVC functions.  However, in some cases, it specifically goes out of the way to provide an easier interface for developers.  For example, let&#8217;s say you&#8217;re implementing a series of actions which return JSON encoded objects.  In many frameworks, this is taken care of within the model, provided back to the controller and sent to the browser.  In the Fever Framework, you have a few options:</p>
<p><em>Controller-level</em></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> MyBlog <span style="color: #000000; font-weight: bold;">extends</span> \Fever\Controller\Json
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPost<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$posts</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BlogPosts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$blog</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findByPrimaryKey</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The above example is a controller which extends the Json controller.  The &#8220;BlogPosts&#8221; class is a model which extends the \Fever\Db\ActiveRecord class.  The &#8220;findByPrimaryKey&#8221; is a method provided within AR.  Once the object is returned, the Json controller handles the JSON encoding and sending proper HTTP headers to the client.</p>
<p><em>Model-level</em></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> BlogPosts <span style="color: #000000; font-weight: bold;">extends</span> \Fever\Db\ActiveRecord
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getBlogPost<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> parent<span style="color: #339933;">::</span><span style="color: #004000;">findByPrimaryKey</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setData</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toJson</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'db'</span> <span style="color: #339933;">=&gt;</span> \MyApp\Db<span style="color: #339933;">::</span><span style="color: #004000;">getReadInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This example returns the data already JSON encoded to the caller.  It extends the ActiveRecord class, set&#8217;s the data via &#8220;setData&#8221; (provide in DataModel), and chains &#8220;toJson&#8221; to the request.  This returns the JSON encoded string needed.</p>
<p>This is only two of many ways to achieve the above goal.  Design theory and flexibility aren&#8217;t mutually exclusive, and flexibility is just as important as design.</p>
<p><strong>Latest PHP  functionality</strong></p>
<p>As you&#8217;ve already noticed, I&#8217;ve incorporated namespaces into the framework.  This is especially nice when editing inside an IDE and you can dive down into each namespace to find code (remember the frameworks that use hack namespace styles that make the class name 50+ characters long?).  The naming convention is easy to read and makes sense!</p>
<p><strong>Speed</strong></p>
<p>The Fever Framework can be used as a set of libraries, but I&#8217;ve taken the liberty to assume you&#8217;ll be using autoloading.  There is a provided class, Fever\Autoloader which takes care of the inclusion of files for classes/namespaces.  Since FF is setup to so that each class falls under a directory based on it&#8217;s namespace, autoloading is fast and efficient!  With that in mind, there is an important feature (some might argue) you should keep in mind.</p>
<p>Each class comes with a comment block that contains all of the file dependencies as require_once statements.  If you so choose, you can remove the comment block and everything will be just fine.  However, keep in mind that each require_once call makes a full file scan.  This is extremely unoptimized!</p>
<p>When you get the code base , you&#8217;ll also notice that a printed stack trace will be relatively short.  You won&#8217;t have 30 classes printed out that are abstracts of abstracts of abstracts of interfaces.  Like I said before, design theory isn&#8217;t the most important feature!</p>
<p><strong>Models</strong></p>
<p>As a PHP developer, this is likely where you&#8217;ll spend most of your time.  Like Zend Framework, Fever doesn&#8217;t come with a abstract model.  It provides a subset of classes for you to decide to use.  For example, if you&#8217;re working at the table level, you might choose to use Fever\Db\ActiveRecord, which provides most of the functionality of the Active Record pattern.  If you are only using stored procedures, Fever\Db\Procedure might be your choice.  But databases aren&#8217;t the only types of models.  What if you&#8217;re working with a SOAP client?  You could extend the Fever\Soap\Client, which provides functionality related to SOAP.</p>
<p><strong>Modules</strong></p>
<p>Module-based application development is included.  If you choose to use this, you simply add a couple of lines of code to your global bootstrap.  If not, do nothing!</p>
<p>Each module is allowed to have its own bootstrap.  That means you can customize it as a separate application.  You can also define how modules are recognized.  If each module is a subdomain, you simply provide that match to the router, and it will recognize it.  If it&#8217;s a subdirectory, do the same!</p>
<p><strong>Views</strong></p>
<p>To provide some options, the framework comes with both a standard PHP view type as well as Smarty.  By default, the standard PHP type is used.  This is easily changed within the bootstrap of your application, or even module.  Each view type comes with a set of helpers as well.</p>
<h3>How do I get the code?</h3>
<p>The alpha review will start on February 19th.  This will include developer code reviews and testing.  This circle will be relatively small (10 &#8211; 20 developers) and will last one week.  The public beta will begin on February 26th, and a download will be available here.</p>
<p>If you are interested in joining the alpha review, please email me at will[at]phpfever[dot]com or <a href="/contact-me">use the contact form</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/introducing-the-php-fever-framework.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Nashville PHPUG Symfony Speakers Announced</title>
		<link>http://www.willfitch.com/nashville-phpug-symfony-speakers-announced.html</link>
		<comments>http://www.willfitch.com/nashville-phpug-symfony-speakers-announced.html#comments</comments>
		<pubDate>Fri, 03 Apr 2009 00:45:29 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[nashville php users group]]></category>
		<category><![CDATA[nashvillephp]]></category>

		<guid isPermaLink="false">http://www.phpfever.com/?p=171</guid>
		<description><![CDATA[On April 24th, Travis Black and Brent Shaffer of <a href="http://www.centresource.com">Centre{source}</a> will be presenting the Symfony PHP framework.  This is our second demonstration after having Shawn McCool present <a href="http://www.codeigniter.com">CodeIgniter</a> last month.  We are excited to have Centre{source} and look forward to their demonstration.]]></description>
			<content:encoded><![CDATA[<p>On April 24th, Travis Black and Brent Shaffer of <a href="http://www.centresource.com">Centre{source}</a> will be presenting the Symfony PHP framework.  This is our second demonstration after having Shawn McCool present <a href="http://www.codeigniter.com">CodeIgniter</a> last month.  We are excited to have Centre{source} and look forward to their demonstration.</p>
<p><strong>Presenter Bios</strong></p>
<p><strong>Travis Black, Developer, Centre{source}</strong><br />
<img src="http://www.phpfever.com/wp-content/uploads/2009/04/tblack.jpg" alt="Travis Black" style="padding:5px;" align="left" /> Travis Black is a software developer currently employed centre{source} interactive strategies. He has been developing web applications in php since 2004. After some experience developing in Ruby, using both the Rails and Merb frameworks, he spent a good deal of time attempting to find a php alternative. In the end, he found symfony, and fell in love. Travis and symfony were married last year and have lived together happily ever since.</p>
<p><strong>Brent Shaffer, Developer, Centre{source}</strong><br />
<img src="http://www.phpfever.com/wp-content/uploads/2009/04/bshaffer-150x150.jpg" alt="Brent Shaffer" style="padding:5px;" align="left"  /> Brent Shaffer is a symfony developer and architect at centre{source} interactive strategies.  He graduated from Belmont University in 2008 with a degree in Commercial Music Technology and a minor in Computer Science.  He has experience with the .NET framework, but has seen the light, and has been a symfony zealot ever since.  He has also done some other cool stuff.</p>
<p><br/><br/><br />
<strong>The meeting location will be announced next week once I work out the sponsorship details.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/nashville-phpug-symfony-speakers-announced.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>April Nashville PHP Users Group Meeting</title>
		<link>http://www.willfitch.com/april-nashville-php-users-group-meeting.html</link>
		<comments>http://www.willfitch.com/april-nashville-php-users-group-meeting.html#comments</comments>
		<pubDate>Wed, 25 Mar 2009 02:45:37 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[nashville php users group]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[symfony]]></category>

		<guid isPermaLink="false">http://www.phpfever.com/?p=167</guid>
		<description><![CDATA[The April 2009 users group meeting will focus on the Symfony framework.  We will be receiving a presentation from <a href="http://www.centresource.com">Centre{source}</a> where one of the developers is on the Symfony development team.  More details to come later.]]></description>
			<content:encoded><![CDATA[<p>The April 2009 users group meeting will focus on the Symfony framework.  We will be receiving a presentation from <a href="http://www.centresource.com">Centre{source}</a> where one of the developers is on the Symfony development team.  More details to come later.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/april-nashville-php-users-group-meeting.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>The Observer Pattern: OOP Techniques in PHP</title>
		<link>http://www.willfitch.com/the-observer-pattern-oop-techniques-in-php.html</link>
		<comments>http://www.willfitch.com/the-observer-pattern-oop-techniques-in-php.html#comments</comments>
		<pubDate>Mon, 09 Mar 2009 20:23:04 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[observer]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php5]]></category>

		<guid isPermaLink="false">http://www.phpfever.com/?p=149</guid>
		<description><![CDATA[The observer pattern provides another way to maintain loose coupling within your code.  It's an extremely simple pattern and is implemented similarly across languages.  There are two parts: the observer and the observable object.  Let's address them both starting with the observer.]]></description>
			<content:encoded><![CDATA[<p>The observer pattern provides another way to maintain loose coupling within your code.  It&#8217;s an extremely simple pattern and is implemented similarly across languages.  There are two parts: the observer and the observable object.  Let&#8217;s address them both starting with the observer.</p>
<p><strong>The Observer</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">interface</span> IObserver
<span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * Method called on event change
	 *
	 * @param mixed $sender
	 * @param mixed $args
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onChange<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sender</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>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>As you can see from above, the observer implements a single method called onChange.  This method is executed when the observable class changes.  The first parameter is typically the instance of the class changing and the second arguments relative to the change.  Now let&#8217;s look at the IObservable interface:</p>
<p><strong>The Observable Object</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">interface</span> IObserverable
<span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * Add an observer
	 *
	 * @param mixed $obj
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addObserver<span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The observable class provides a method for one or more observers to be notified.  The observable class needs to maintain a list of these observers and notify them when necessary.  A common usage of this is a logging mechanism that needs to be notified when data is created, updated and deleted.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> UserLogging implements IObserver
<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onChange<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sender</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: #666666; font-style: italic;">// Log the changes (we echo here)</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class is '</span><span style="color: #339933;">.</span><span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sender</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' and the arguments are '</span><span style="color: #339933;">.</span><span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</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> User implements IObserverable
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_observers</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;">private</span> <span style="color: #000000; font-weight: bold;">function</span> notifyObservers<span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$count</span><span style="color: #339933;">=</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>_observers<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			 <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$count</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</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>_observers<span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">onChange</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</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>
	<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> deleteUser<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteUser</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">notifyObservers</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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> addObserver<span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Add logic for interface implementation check here if you want.</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_observers<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addObserver</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> UserLogging<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;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteUser</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">55</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Not only does this provide you some pseudo event access, but this is a much cleaner, maintainable implementation of observing a particular object.</p>
<p><strong>Conclusion</strong></p>
<p>I hope you find ways at work to implement this use pattern.  We more often than not get caught up with getting code written (it comes with the territory) and lose sight of these power and proven designs to get the job done.  I&#8217;m certainly just as much to blame as the next developer! Hope this helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/the-observer-pattern-oop-techniques-in-php.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

