<?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>phpCodeWorks.com Blog</title>
	<atom:link href="http://www.phpcodeworks.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phpcodeworks.com/blog</link>
	<description></description>
	<lastBuildDate>Fri, 23 Mar 2012 17:10:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP Micro-Framework Roundup</title>
		<link>http://www.phpcodeworks.com/blog/2012/03/php-micro-framework-roundup/</link>
		<comments>http://www.phpcodeworks.com/blog/2012/03/php-micro-framework-roundup/#comments</comments>
		<pubDate>Sat, 17 Mar 2012 20:29:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[fat-free]]></category>
		<category><![CDATA[flightphp]]></category>
		<category><![CDATA[limonade]]></category>
		<category><![CDATA[micro-frameworks]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[silex]]></category>
		<category><![CDATA[slim]]></category>

		<guid isPermaLink="false">http://www.phpcodeworks.com/blog/?p=70</guid>
		<description><![CDATA[I stumbled on what folks are calling micro-frameworks a few months ago when searching for an easier way to create RESTful services.  I was using Zend Framework at the time with Zend_Rest_Route and Zend_Controller_Rest.  That worked fine, except that it was super slow for something so simple, and there was no way to [...]]]></description>
			<content:encoded><![CDATA[<p>I stumbled on what folks are calling micro-frameworks a few months ago when searching for an easier way to create RESTful services.  I was using Zend Framework at the time with Zend_Rest_Route and Zend_Controller_Rest.  That worked fine, except that it was super slow for something so simple, and there was no way to handle hierarchical resources.  For example it was impossible to route requests to something like: /folder/3/file/2.  Instead one would have to create a resource like /folder-files/2/?folder_id=3.  Thugly.</p>
<p>Anyway, I knew that all the cool Ruby kids were using Sinatra &#8211; at least for prototyping, and so I started looking for Sinatra inspired PHP frameworks, and guess what?  There are lots of them!  Not surprisingly, they appear very similar to one another in a Sinatra-ish sort of way.  Most of them allow one to define a resource and its implementation using lambdas.  For example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$app</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> App<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/folder/{folder_id}/file/{file_id}'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$folder_id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$file_id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Hello World!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>After experimenting with a number of these frameworks, I thought I&#8217;d give my initial impressions along with some benchmarks and any interesting contextual information I found.</p>
<h3>The Players:</h3>
<h4>1. <a title="Silex - PHP Micro-Framework" href="http://silex.sensiolabs.org" target="_blank"><code>Silex</code></a> (Requires PHP &gt;= 5.3):</h4>
<p>The Silex project is led by the ever prolific Fabien Potencier, the creator of the Symphony framework.  Silex is available under an MIT License, and has commercial backing from Sensio Labs &#8211; which also backs Symphony, Doctrine and Twig to name a few.</p>
<p>Silex has a couple of interesting features that are unique among PHP micro-frameworks.  First is its simple and elegant DI container based on Pimple, another Sensio Labs product.  The Silex Application class extends Pimple, which is an implementation of the ArrayAccess interface that has been part of PHP since v5.0.  Doing this makes it possible to use an instance of its Application class as if it were an array.  For example, you can attach any object to an Application object with the following syntax:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$app</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Silex\Application<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$app</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'config'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Config<span style="color: #009900;">&#40;</span><span style="color: #000088;">$config_path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This gives us an incredibly easy way to inject dependencies when testing, or using the application in other environments, such as the command line.</p>
<p>The other unique aspect of Silex is that it is built on a number of Symphony2 components, which are included in a phar distribution.  These include an autoloader, and request and response classes that make handling things like json and http headers much easier.  There is decent but not extensive documentation available on their website.</p>
<h4>2. <a href="http://www.slimframework.com/" target="_blank">Slim</a> (Requires PHP &gt;= 5.1)</h4>
<p>Slim takes a minimalist approach while providing much of what you need to get started.  It supports hooks for executing code at different points in its life-cycle, and tie-ins for Rack-like middleware.  In addition, it contains classes for managing requests, responses, cookies, logging, views, HTTP caching, and more.  Because Slim supports PHP &gt;= 5.1, it does not utilize namespaces or lambdas in its library code.  However, it allows for the use of lambdas when defining routes as long as you&#8217;re running PHP &gt;= 5.3.  The following is an example of defining routes with and without lambdas.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'Slim/Slim.php'</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000088;">$app</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Slim<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/hello/:name'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Hello, <span style="color: #006699; font-weight: bold;">$name</span>!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/hello2/:name'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'hello2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> hello2<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Hola <span style="color: #006699; font-weight: bold;">$name</span>!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The Slim framework has well organized and thorough documentation, and it has an active user base with over a thousand users watching it on github.</p>
<h4>3. <a href="http://limonade-php.github.com/" target="_blank">Limonade</a> (Requires PHP &gt;= 5.1.6, may work with older versions)</h4>
<p>Limonade is the oldest of the Sinatra inspired frameworks.  Rather than defining an application class, Limonade&#8217;s single library file defines a number of functions that you use to define routes and run the application.  Here&#8217;s what that looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'vendors/limonade.php'</span><span style="color: #339933;">;</span>
&nbsp;
dispatch<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'hello'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> hello<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;">'Hello world!'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This may be terse, but it seems messy to rely on global functions, especially considering the number of functions limonade.php defines.  If you can get past this design decision, the rest of the library looks well written, and because its been out there for a while, it appears quite mature.  The framework provides hooks and filters, as well as a view component that supports layouts.  Additionally, it has good documentation on its github page.</p>
<h4>4. <a href="http://flightphp.com/" target="_blank">Flight</a> (Requires PHP &gt;= 5.3)</h4>
<p>Everything about Flight appears simple and elegant, including its website and documentation.  It is a bare-bones framework that takes a somewhat unique approach in a couple of ways.  The most obvious is that it uses static methods, and so does not require that the application class be instantiated.  Here&#8217;s what a sample app looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'flight/Flight.php'</span><span style="color: #339933;">;</span>
&nbsp;
Flight<span style="color: #339933;">::</span><span style="color: #004000;">route</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'hello world!'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Flight<span style="color: #339933;">::</span><span style="color: #004000;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>A lot of people decry even the liberal use of static methods, but it makes sense when all that is needed is a single instance of something.  It does make it impossible to implement the ArrayAccess interface that Silex makes use of, but Flight does something similar with its register method which allows you to map classes to a static method that will in turn return a shared instance of the class you specify.  For example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Register class with constructor parameters</span>
Flight<span style="color: #339933;">::</span><span style="color: #004000;">register</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'db'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Database'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'mydb'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'user'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'pass'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get an instance of your class</span>
<span style="color: #666666; font-style: italic;">// This will create an object with the defined parameters</span>
<span style="color: #666666; font-style: italic;">//</span>
<span style="color: #666666; font-style: italic;">//     new Database('localhost', 'mydb', 'user', 'pass');</span>
<span style="color: #666666; font-style: italic;">//</span>
<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> Flight<span style="color: #339933;">::</span><span style="color: #004000;">db</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The first parameter is the name of the method to create, the second is a class name, and the third is an array of parameters.  Flight::db() will subsequently lazy load the Database class the first time it is called, then return a shared instance on each additional invocation.</p>
<p>The second unique feature is what the author calls filters, which can be executed before and after any function, and can change both the parameters and output of the function they&#8217;re attached to.  This feature is in place of the hooks that other frameworks use to inject code into different parts of the application life-cycle.  Here&#8217;s an example from the Flight documentation.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Map a custom method</span>
Flight<span style="color: #339933;">::</span><span style="color: #004000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hello'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Hello, <span style="color: #006699; font-weight: bold;">$name</span>!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Add a before filter</span>
Flight<span style="color: #339933;">::</span><span style="color: #004000;">before</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hello'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span><span style="color: #000088;">$params</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span><span style="color: #000088;">$output</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Manipulate the parameter</span>
    <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Fred'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Add an after filter</span>
Flight<span style="color: #339933;">::</span><span style="color: #004000;">after</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hello'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span><span style="color: #000088;">$params</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span><span style="color: #000088;">$output</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Manipulate the output</span>
    <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot; Have a nice day!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Invoke the custom method</span>
<span style="color: #b1b100;">echo</span> Flight<span style="color: #339933;">::</span><span style="color: #004000;">hello</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Bob'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Additionally, Flight contains views with layouts, request and response objects and http caching.  All in all it appears to be a well executed and innovative framework.</p>
<h4>5. <a href="http://fatfree.sourceforge.net/" target="_blank">Fat-Free Framework</a> (Requires PHP &gt;= 5.3)</h4>
<p>Coming soon . . .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpcodeworks.com/blog/2012/03/php-micro-framework-roundup/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Traits: PHP 5.4&#8217;s Must Have Feature</title>
		<link>http://www.phpcodeworks.com/blog/2011/12/traits-php-5-4s-must-have-feature/</link>
		<comments>http://www.phpcodeworks.com/blog/2011/12/traits-php-5-4s-must-have-feature/#comments</comments>
		<pubDate>Wed, 14 Dec 2011 22:53:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP News & Features]]></category>

		<guid isPermaLink="false">http://blog.phpcodeworks.com/?p=55</guid>
		<description><![CDATA[PHP 5.4&#8217;s most anticipated feature &#8220;Traits&#8221; is a mechanism for horizontal code re-use that provides a way to include a set of methods in multiple classes at compile time.  Why do we need an alternative to composition, our current mechanism for horizontal code re-use?   The problem with composition is that everything happens [...]]]></description>
			<content:encoded><![CDATA[<p>PHP 5.4&#8217;s most anticipated feature &#8220;Traits&#8221; is a mechanism for horizontal code re-use that provides a way to include a set of methods in multiple classes at compile time.  Why do we need an alternative to composition, our current mechanism for horizontal code re-use?   The problem with composition is that everything happens at run-time, and whenever we use composition, we create a dependency that must be managed.  When we attach a Trait to a class, it becomes a part of that class, the way a parent class is a part of its child, only the relationship is not hierarchical.  This means that methods that are attached to a class with Traits can be tested like any other method in the class.</p>
<p>The essential characteristics of Traits are these:  they are blocks of code that cannot be instantiated, they have a mechanism for controlling visibility and resolving conflict, and they can be nested.  That is, a Trait may be composed partially or entirely of other Traits.  Let&#8217;s take a look at the particulars of these features.</p>
<p>A developer begins by defining methods in a block preceded by the keyword &#8220;trait&#8221; and an identifier.  Once you&#8217;ve defined a trait, you import its methods into a class with the keyword &#8220;use&#8221; followed by the trait name.  You can import more than one trait per &#8220;use&#8221; statement by including multiple trait names separated by commas.  Additionally, you can can limit which methods are imported by defining a block following the trait names.  The traits feature includes conflict detection, and syntax for indicating which methods to use when you attempt to use more than one trait with the same method.  Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">trait bigBar <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> toString<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;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">', '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> bar<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;">'Big '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_bar<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
trait littleBar <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> bar<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;">'Little '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_bar<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> foo <span style="color: #009900;">&#123;</span>
    use bigBar<span style="color: #339933;">,</span> littleBar <span style="color: #009900;">&#123;</span>
        bigBar<span style="color: #339933;">::</span><span style="color: #004000;">bar</span><span style="color: #339933;">;</span>
        littleBar<span style="color: #339933;">::</span><span style="color: #004000;">bar</span> <span style="color: #b1b100;">as</span> smallBar<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The first line inside the &#8220;use&#8221; block resolves the conflict between the bar methods present in both traits.  The second line renames littleBar&#8217;s &#8220;bar&#8221; method to smallBar.  This makes it possible to use both &#8220;bar&#8221; methods.</p>
<p>I can imagine horizontal code reuse being messy if one were allowed to import methods from any class into any other class, but the syntax of Traits ensures that developers only use components that are designed for reuse, rather than piecing them together from a variety of sources.  Traits will make code easier to maintain, not only because it simplifies re-use in a number of contexts, but also because it simplifies testing.</p>
<p>This is the second minor release of PHP that has added the kind of language features that are commonly associated with major releases.  Version 5.3 introduced both namespaces and lambdas, and with the addition of Traits, PHP has gained three truly groundbreaking features in two minor version updates.  It will be interesting to see whether the PHP team continues to add major language features with minor releases.</p>
<p>You can read more about Traits here:<br />
<a href="https://wiki.php.net/rfc/horizontalreuse">https://wiki.php.net/rfc/horizontalreuse</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpcodeworks.com/blog/2011/12/traits-php-5-4s-must-have-feature/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Expand an array into function arguments with call_user_func_array</title>
		<link>http://www.phpcodeworks.com/blog/2009/08/automatically-expand-an-array-into-function-parameters/</link>
		<comments>http://www.phpcodeworks.com/blog/2009/08/automatically-expand-an-array-into-function-parameters/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 01:41:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>

		<guid isPermaLink="false">http://www.phpcodeworks.com/?p=17</guid>
		<description><![CDATA[call_user_func_array is a time saving, though not commonly used function that allows you to treat the items of an array as arguments when calling a function.   You indicate which function call_user_func_array is to call with the first argument, which can take two or three forms, depending on whether you&#8217;re using a version of [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://us2.php.net/manual/en/function.call-user-func-array.php" target="_blank">call_user_func_array</a> is a time saving, though not commonly used function that allows you to treat the items of an array as arguments when calling a function.   You indicate which function call_user_func_array is to call with the first argument, which can take two or three forms, depending on whether you&#8217;re using a version of PHP greater or equal to 5.3.  </p>
<p>In it&#8217;s first form, you supply a function name as the first argument.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> sum<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> z<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> x <span style="color: #339933;">+</span> y <span style="color: #339933;">+</span> z<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> 
&nbsp;
<span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">call_user_func_array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sum'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">// prints 9</span></pre></div></div>

<p>In the second form, you pass an array containing an object reference and the name of a method as the first argument.  The following would call $obj->sum.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">call_user_func_array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'sum'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The third form involves the use of a lambda, and so it only works with versions of PHP >= 5.3.  In this form you simply define the lambda, and use its reference as the first parameter.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$func</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> z<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> x <span style="color: #339933;">+</span> y <span style="color: #339933;">+</span> z<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">call_user_func_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$func</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">// prints 14</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.phpcodeworks.com/blog/2009/08/automatically-expand-an-array-into-function-parameters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
