<?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>Dan Dean &#187; PHP</title>
	<atom:link href="http://dandean.com/category/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://dandean.com</link>
	<description></description>
	<lastBuildDate>Wed, 12 May 2010 04:50:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Simulate Network Delays</title>
		<link>http://dandean.com/category/php/2008/simulate-network-delays/</link>
		<comments>http://dandean.com/category/php/2008/simulate-network-delays/#comments</comments>
		<pubDate>Tue, 19 Feb 2008 00:37:05 +0000</pubDate>
		<dc:creator>Dan Dean</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://dandean.com/category/net/2008/simulate-network-delays/</guid>
		<description><![CDATA[When developing web applications it is helpful to be able to simulate a slow network. This can help you see how your UI indicators, ajax loaders, etc., will operate in various network situations, unlike your local network which is generally free of any sort of interference. These little snippets of code will freeze the execution [...]]]></description>
			<content:encoded><![CDATA[<p>When developing web applications it is helpful to be able to simulate a slow network. This can help you see how your UI indicators, ajax loaders, etc., will operate in various network situations, unlike your local network which is generally free of any sort of interference.</p>
<p>These little snippets of code will freeze the execution of your web application for three seconds:</p>
<pre class="brush: php; gutter: false;">
// PHP
sleep(3);
</pre>
<pre class="brush: csharp; gutter: false;">
// .Net
System.Threading.Thread.Sleep(3000);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://dandean.com/category/php/2008/simulate-network-delays/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fluent: PHP Data Types as Fluent Objects</title>
		<link>http://dandean.com/category/code/2007/fluent-php-data-types-as-fluent-objects/</link>
		<comments>http://dandean.com/category/code/2007/fluent-php-data-types-as-fluent-objects/#comments</comments>
		<pubDate>Sun, 28 Oct 2007 16:19:09 +0000</pubDate>
		<dc:creator>Dan Dean</dc:creator>
				<category><![CDATA[Code/Projects]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://dandean.com/category/code/2007/fluent-php-data-types-as-fluent-objects/</guid>
		<description><![CDATA[Introducing Fluent, a set of PHP 5 classes that make writing code happier. Fluent enables you to deal with data types as objects in the fluent api style, like you can in most other modern programming languages. // JavaScript &#34;Hello There&#34;.toUpperCase().split(' '); // C# &#34;Hello There&#34;.ToUpper().Split(new Char[] {' '}); # Ruby &#34;Hello There&#34;.upcase.split(' ') # [...]]]></description>
			<content:encoded><![CDATA[<p>Introducing <strong>Fluent</strong>, a set of <a href="http://www.php.net/manual/en/language.oop5.php">PHP 5</a> classes that make writing code happier. Fluent enables you to deal with data types as objects in the <a href="http://www.alexatnet.com/node/98">fluent</a> <a href="http://www.travisswicegood.com/index.php/2007/10/26/fluent_api_here_i_come">api</a> style, like you can in most other modern programming languages.</p>
<pre class="brush: jscript; gutter: false;">
// JavaScript
&quot;Hello There&quot;.toUpperCase().split(' ');
</pre>
<pre class="brush: csharp; gutter: false;">
// C#
&quot;Hello There&quot;.ToUpper().Split(new Char[] {' '});
</pre>
<pre class="brush: ruby; gutter: false;">
# Ruby
&quot;Hello There&quot;.upcase.split(' ')
</pre>
<pre class="brush: python; gutter: false;">
# Python
&quot;Hello There&quot;.upper().split()
</pre>
<pre class="brush: php; gutter: false;">
// PHP
explode(' ', strtoupper('Hello There'));

// PHP using Fluent
S('Hello There')-&gt;toupper()-&gt;explode(' ');
</pre>
<p><span id="more-13"></span></p>
<h2>Fluent Data Types</h2>
<h3>OMG! &#8220;Strings!&#8221;</h3>
<p>Using the <strong>Fluent_String</strong> class you can chain your string manipulation functions together on to one line, much like a sentence.</p>
<pre class="brush: php;">
// Trim the white space from the left and right; Set to title case;
echo S('    fluent data type, huzaa!!   ')-&gt;trim()-&gt;ucwords();

// PRINTS: &quot;Fluent Data Type, Huzaa!!&quot;

// The same as above, but replace whitespace with 3 exclamations
echo S('    fluent data type, huzaa!!   ')-&gt;trim()-&gt;ucwords()
	-&gt;preg_replace(&quot;/s+/&quot;,'!!!');

// PRINTS: &quot;Fluent!!!Data!!!Type,!!!Huzaa!!&quot;;
</pre>
<h3>Neat, simple, arrays</h3>
<p>The <strong>Fluent_Array</strong> class works just like the above string class. Splice, shift, count, asort, whatever, at will.</p>
<pre class="brush: php;">
echo A(array('Bush','Clinton'))-&gt;splice(2,0,array('Bush','Clinton'))
	-&gt;push('Please stop now.');

// PRINTS: &quot;Bush, Clinton, Bush, Clinton, Please stop now.&quot;

echo A(array('Bush','Clinton'))-&gt;splice(2,0,array('Bush','Clinton'))
	-&gt;push('Please stop now.')-&gt;unique();

// PRINTS: &quot;Bush, Clinton, Please stop now.&quot;
</pre>
<h3>Numbers&#8230;</h3>
<p>Same goes for numbers, except that both <code>int</code> and <code>float</code> are handled within a single class: <strong>Fluent_Number</strong>.</p>
<pre class="brush: php;">
// Find the circumference of a circle which has a radius of 3.5cm
echo N(3.5)-&gt;times(2)-&gt;times(M_PI);

// PRINTS: 21.991148575129

// Find the radius of a circle based on circumference
echo N(21.991148575129)-&gt;divide(M_PI)-&gt;divide(2)-&gt;round(1);

// PRINTS: 3.5
</pre>
<h3>Boolean === Complete</h3>
<p>To round it out, the Boolean data type is also supported. This is in place for completeness, so that when you&#8217;re dealing with a Fluent object, you&#8217;re always returned a Fluent object.</p>
<pre class="brush: php;">
echo B(false);

// PRINTS: false
</pre>
<h3>Core Data</h3>
<p>You might have asked by now: &#8220;what if I <em>want the actual piece if data</em> instead of the Fluent object?&#8221; Well, the data at the core of every object is always available to you via the &#8216;value&#8217; property of the Fluent object, so it&#8217;s easy to leave the object behind and pass on the actual data.</p>
<pre class="brush: php;">
echo O(5)-&gt;value;

// PRINTS: 5
</pre>
<h2>Caveats</h2>
<p>There are, of course, some caveats that could trip you up, especially when dealing with numbers. For instance, if you try to directly add to Fluent_Number objects you&#8217;ll be given an E_NOTICE, the objects will be incorrectly &#8216;added&#8217;, and you&#8217;ll be sent on your way:</p>
<pre class="brush: php;">
echo $bad = N(10) + N(20);
Notice: Object of class Fluent_Number could not be converted to int

// PRINTS: 2
</pre>
<h3>Uncaveat</h3>
<p>The correct way is to use operators on the value of an object is to directly work on the <strong>core data</strong> of the object:</p>
<pre class="brush: php;">
echo $good = N(10)-&gt;value + N(20)-&gt;value; // return type = int
echo $good = N(10)-&gt;plus(20);             // return type = Fluent_Number

// PRINTS: 30
</pre>
<h2>Trans<em>mogrify</em> that shit</h2>
<p>It&#8217;s very common that a single piece of data will need to be different types of data at different points in your application. Fluent objects automatically and intelligently convert your object from one Fluent Data Type to the next based on the data type of the return value, so you won&#8217;t have to create new Fluent objects whenever your data changes form:</p>
<pre class="brush: php;">
echo O('President Bush is an evil bigot.')      // string
	-&gt;explode(' ')                          // array
	-&gt;splice(1,1,array('Cheney'))           // array
	-&gt;join(' ');                            // string

// PRINTS: President Cheney is an evil bigot.

echo O('President Bush is an evil bigot.')      // string
	-&gt;explode(' ')                          // array
	-&gt;splice(1,1,array('Cheney'))           // array
	-&gt;join(' ')                             // string
	-&gt;rev();                                // string

// PRINTS: .togib live na si yenehC tnediserP

echo O('President Bush is an evil bigot.')      // string
	-&gt;explode(' ')                          // array
	-&gt;splice(1,1,array('Cheney'))           // array
	-&gt;join(' ')                             // string
	-&gt;rev()                                 // string
	-&gt;len()                                 // int
	-&gt;times(19.5)                           // int
	-&gt;plus(3);                              // int

// PRINTS: 666
</pre>
<h2>Oh PHP, you&#8217;re so crazy.</h2>
<p>I can&#8217;t really say that PHP was the most <em>well</em> thought out thing in the world, and that&#8217;s probably because it wasn&#8217;t really thought out. PHP has adapted as a language as the web has grown and changed, which has enabled people of all skill level to get right in and create amazing tools. The language is incredibly simple but capable of astounding complexity.</p>
<p>This, of course, has its down side: when it comes to <strong>function names</strong>, <strong>function argument order</strong>, and <strong>function return values</strong> some crazy ass shit went down.</p>
<p>Fluent objects keep those unsightly stretch marks out of view by automatically shortening function names to their succinct names, among other things.</p>
<h3><code>array_splice()</code>, your name sucks</h3>
<p>If you&#8217;ve been looking closely you&#8217;ll have noticed that instead of <code>strtoupper()</code>, <code>strrev()</code> and <code>array_splice()</code> we&#8217;ve been using <code>splice()</code>, <code>toupper()</code> and <code>rev()</code>. This feature is universally applied to all functions with name prepended by &#8216;str&#8217;, str_&#8217; and &#8216;array_&#8217;.</p>
<h3>What kind of order is this?</h3>
<p>Another incredibly annoying thing about PHP is that there seems to be no logic to how the order of arguments was applied accross functions. The subject of the function, depending on the function, could be either the 1st, 2nd, or 3rd argument passed in.</p>
<p>With Fluent objects, the subject of the function is always the data within the Fluent object itself. This means that when you&#8217;re reading the documentation at php.net, remember that you should refrain from passing the subject as an argument&#8230; just pretend it doesn&#8217;t exist.</p>
<pre class="brush: php;">
// Normal sprintf
echo sprintf('Hello %s.','There');

// Fluent sprintf
echo S('Hello %s.')-&gt;sprintf('There');

// Multi-chained Fluent sprintf
echo S('Hello %s.')-&gt;sprintf('There %s')-&gt;sprintf('Sucka');

// Normal str_replace
echo str_replace(')','(','Sad :)');

// Fluent str_replace
echo S('Sad :)')-&gt;replace(')','(');
</pre>
<h3>What are you looking at?</h3>
<p>What makes Fluent objects so valuable is our ability to chain function calls together indefinitely. In order to do this we always need to have a handle on what we&#8217;re dealing with. PHP throws us for a loop here because many of the array functions require that the subject array be passed in as an argument <a href="http://docs.php.net/language.references.pass" title="Passing by Reference" target="_blank">by reference</a>, using the <code>&amp;</code> operator. This then modifies the array you passed in, and returns a result, which is in many cases <em>not</em> the modified array but a boolean &#8220;success&#8221; indicator, a portion of the original array, the length of the resulting array, and so on.</p>
<p>Our Fluent objects solve this problem by always returning the modified array while at the same time populating the <code>native</code> property of the Fluent object with the value usually returned by the native PHP function.</p>
<pre class="brush: php;">
$fruit = array('apple','orange','kiwi');
$result = array_push($fruit,'banana');

var_dump($fruit);

// PRINTS: array(4) {
//   [0]=&gt;string(5) &quot;apple&quot;
//   [1]=&gt;string(6) &quot;orange&quot;
//   [2]=&gt;string(4) &quot;kiwi&quot;
//   [3]=&gt;string(6) &quot;banana&quot;
// }

var_dump($result);
PRINTS: int(4)
</pre>
<p>Compare that to how a Fluent object behaves:</p>
<pre class="brush: php;">
$fruit = A(array('apple','orange','kiwi'))-&gt;push('banana');

var_dump($fruit-&gt;value);

// PRINTS: array(4) {
//   [0]=&gt;string(5) &quot;apple&quot;
//   [1]=&gt;string(6) &quot;orange&quot;
//   [2]=&gt;string(4) &quot;kiwi&quot;
//   [3]=&gt;string(6) &quot;banana&quot;
// }

var_dump($fruit-&gt;native-&gt;value);

// PRINTS: int(4)
</pre>
<h2>Shorter is Better</h2>
<p>I created these classes to make programming in PHP quicker and more elegant. For this reason you don&#8217;t have to use <code>new Fluent_String()</code> to start using the string class. You can if you want, of course, but each data type already has its own one letter shortcut function: <code>A()</code>, <code>S()</code>, <code>N()</code>, and <code>B()</code>.</p>
<p>There is also one more shortcut function for when you don&#8217;t care what you&#8217;re passing in, you just want it to be a Fluent object: <code>O()</code>. This function will take whatever it is you give it and return the appropriate Fluent data object.</p>
<h2>And&#8230;</h2>
<p>That&#8217;s basically it, really. Please <a href="http://dandean.com/wp-content/uploads/2007/10/fluent.zip" title="Fluent">download it and try it out</a>. If you do, let me know what you think, where it could be improved upon, and if there are any stupid bugs that I missed.</p>
<p>In the future I plan to deal with a couple outstanding issues. First I&#8217;ll update Fluent to allow you to provide your own data type classes to use instead of mine (or extended from mine). Also, an important part of proving the usefulness of Fluent will be to figure out how performant they are. So fairly soon you should be able to expect some benchmarks to compare function calls to their native incarnation.</p>
]]></content:encoded>
			<wfw:commentRss>http://dandean.com/category/code/2007/fluent-php-data-types-as-fluent-objects/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
