<?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>Sat, 31 Dec 2011 00:09:12 +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>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[The upcoming release of PHP 5.4 will contain a feature that for many will make the decision to upgrade a no-brainer.  This feature called Traits is a mechanism for horizontal code reuse that provides a way for a set of methods to be used by multiple classes in independent class hierarchies.  Traits gives [...]]]></description>
			<content:encoded><![CDATA[<p>The upcoming release of PHP 5.4 will contain a feature that for many will make the decision to upgrade a no-brainer.  This feature called Traits is a mechanism for horizontal code reuse that provides a way for a set of methods to be used by multiple classes in independent class hierarchies.  Traits gives developers a way to design code blocks that can be plugged into any class at compile time.  This mitigates many of the limitations of a single inheritance model.</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 one class into another, 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.  I see Traits making code easier to maintain, not only because it simplifies reuse in a number of contexts, but also because it will be easier to test code included at compile time.</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 language features with minor releases &#8211; or maybe these point releases shouldn&#8217;t be thought of as minor releases at all.</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>

