<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Fluent Interfaces in PHP</title>
	<atom:link href="http://mikenaberezny.com/2005/12/20/fluent-interfaces-in-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://mikenaberezny.com/2005/12/20/fluent-interfaces-in-php/</link>
	<description></description>
	<pubDate>Fri, 25 Jul 2008 10:30:30 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
		<item>
		<title>By: PHP Fluency &#124; Mullen Mills</title>
		<link>http://mikenaberezny.com/2005/12/20/fluent-interfaces-in-php/#comment-102902</link>
		<dc:creator>PHP Fluency &#124; Mullen Mills</dc:creator>
		<pubDate>Mon, 07 Jul 2008 08:31:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikenaberezny.com/?p=35#comment-102902</guid>
		<description>[...] Mike Naberezny&#8217;s articleis where I started [...]</description>
		<content:encoded><![CDATA[<p>[...] Mike Naberezny&#8217;s articleis where I started [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pablo Morales</title>
		<link>http://mikenaberezny.com/2005/12/20/fluent-interfaces-in-php/#comment-94694</link>
		<dc:creator>Pablo Morales</dc:creator>
		<pubDate>Tue, 27 May 2008 13:32:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikenaberezny.com/?p=35#comment-94694</guid>
		<description>Great explanation. I know this method with Zend_Db_Select :P</description>
		<content:encoded><![CDATA[<p>Great explanation. I know this method with Zend_Db_Select :P</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: This Dev For Hire : Fluent interfaces and code readability</title>
		<link>http://mikenaberezny.com/2005/12/20/fluent-interfaces-in-php/#comment-87400</link>
		<dc:creator>This Dev For Hire : Fluent interfaces and code readability</dc:creator>
		<pubDate>Mon, 07 Apr 2008 08:09:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikenaberezny.com/?p=35#comment-87400</guid>
		<description>[...] to make my case stronger, I will use an example taken directly out of the blog of Mike Naberezny - Fluent Interfaces in PHP. Mike&#8217;s a renown PHP authority, so it should make you [...]</description>
		<content:encoded><![CDATA[<p>[...] to make my case stronger, I will use an example taken directly out of the blog of Mike Naberezny - Fluent Interfaces in PHP. Mike&#8217;s a renown PHP authority, so it should make you [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: This Dev For Hire</title>
		<link>http://mikenaberezny.com/2005/12/20/fluent-interfaces-in-php/#comment-86893</link>
		<dc:creator>This Dev For Hire</dc:creator>
		<pubDate>Mon, 31 Mar 2008 13:58:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikenaberezny.com/?p=35#comment-86893</guid>
		<description>&lt;strong&gt;Fluent interfaces and code readability...&lt;/strong&gt;

Fluent interfaces. Sigh. They help sometimes, they really do. In fact I don't have anything against the idea, it's the actual use that makes me uncomfortable....</description>
		<content:encoded><![CDATA[<p><strong>Fluent interfaces and code readability&#8230;</strong></p>
<p>Fluent interfaces. Sigh. They help sometimes, they really do. In fact I don&#8217;t have anything against the idea, it&#8217;s the actual use that makes me uncomfortable&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick</title>
		<link>http://mikenaberezny.com/2005/12/20/fluent-interfaces-in-php/#comment-55535</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Mon, 22 Oct 2007 17:30:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikenaberezny.com/?p=35#comment-55535</guid>
		<description>I think I explained my thoughts pretty poorly, so I'll try again.  I'll take your above example of a fluent interface to fill out a customer's order.

Think of the process of filling up that order as if it were a physical assembly line in a factory.  The order starts as an empty container headed down a conveyor belt.  At each step, a robot picks up the container, operates on its contents to add, alter, or remove things, and puts the container back on the line.

But when there is a side-effect created by one of those robots, how do we deal with that further down the assembly line?  Maybe the factory is out of "TAL" widgets.  One option would be to fail immediately.  I tend to think that the construction of our customer's order should continue, adding the HPK and LGV widgets.  The robot in charge of adding that TAL widget to the order, should attach a notice with some nice info, perhaps the next expected date of TAL availability.  At the end of my metaphorical assembly line, a quality-control engineer would see that notice and take action, perhaps throwing out the order, perhaps presenting some options to the customer.

It's pretty obvious that Order-&#62;with() can never return null or throw an exception.  These fluent interfaces like phpUnit's mock object API surely make certain that null returns or exceptions will never arise.  So, is it part of the definition of a fluent interface that the chained method calls will never return null, and never throw exceptions?</description>
		<content:encoded><![CDATA[<p>I think I explained my thoughts pretty poorly, so I&#8217;ll try again.  I&#8217;ll take your above example of a fluent interface to fill out a customer&#8217;s order.</p>
<p>Think of the process of filling up that order as if it were a physical assembly line in a factory.  The order starts as an empty container headed down a conveyor belt.  At each step, a robot picks up the container, operates on its contents to add, alter, or remove things, and puts the container back on the line.</p>
<p>But when there is a side-effect created by one of those robots, how do we deal with that further down the assembly line?  Maybe the factory is out of &#8220;TAL&#8221; widgets.  One option would be to fail immediately.  I tend to think that the construction of our customer&#8217;s order should continue, adding the HPK and LGV widgets.  The robot in charge of adding that TAL widget to the order, should attach a notice with some nice info, perhaps the next expected date of TAL availability.  At the end of my metaphorical assembly line, a quality-control engineer would see that notice and take action, perhaps throwing out the order, perhaps presenting some options to the customer.</p>
<p>It&#8217;s pretty obvious that Order-&gt;with() can never return null or throw an exception.  These fluent interfaces like phpUnit&#8217;s mock object API surely make certain that null returns or exceptions will never arise.  So, is it part of the definition of a fluent interface that the chained method calls will never return null, and never throw exceptions?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Naberezny</title>
		<link>http://mikenaberezny.com/2005/12/20/fluent-interfaces-in-php/#comment-55514</link>
		<dc:creator>Mike Naberezny</dc:creator>
		<pubDate>Mon, 22 Oct 2007 15:18:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikenaberezny.com/?p=35#comment-55514</guid>
		<description>No, there is not a workaround.  

&lt;pre&gt;
$ php -r '$o = null; $o-&gt;anything;';
PHP Notice:  Trying to get property of non-object in Command line code on line 1

$ php -r '$o = null; $o-&gt;anything();';
PHP Fatal error:  Call to a member function anything() on a non-object in Command line code on line 1
&lt;/pre&gt;

If you attempt to get a property of a non-object, a notice will be raised.  If you attempt to call a method on a non-object, a fatal error will occur.  

There are few examples of true fluent interfaces in PHP.  One is the PHPUnit mocking API, which you can read about in the &lt;a href="http://www.phpunit.de/pocket_guide/3.0/en/mock-objects.html" rel="nofollow"&gt;Pocket Guide&lt;/a&gt;.  Its methods use appropriate vocabulary that make it a fluent interface as described by Martin Fowler's &lt;a href="http://martinfowler.com/bliki/FluentInterface.html" rel="nofollow"&gt;original article&lt;/a&gt;.  Since it is designed as an actual fluent interface, each method always returns the object handle and you don't have these problems.

Both of your examples above use object chaining but neither is a fluent interface.  It's important to understand the difference and not abuse chaining.</description>
		<content:encoded><![CDATA[<p>No, there is not a workaround.  </p>
<pre>
$ php -r '$o = null; $o->anything;';
PHP Notice:  Trying to get property of non-object in Command line code on line 1

$ php -r '$o = null; $o->anything();';
PHP Fatal error:  Call to a member function anything() on a non-object in Command line code on line 1
</pre>
<p>If you attempt to get a property of a non-object, a notice will be raised.  If you attempt to call a method on a non-object, a fatal error will occur.  </p>
<p>There are few examples of true fluent interfaces in PHP.  One is the PHPUnit mocking API, which you can read about in the <a href="http://www.phpunit.de/pocket_guide/3.0/en/mock-objects.html" rel="nofollow">Pocket Guide</a>.  Its methods use appropriate vocabulary that make it a fluent interface as described by Martin Fowler&#8217;s <a href="http://martinfowler.com/bliki/FluentInterface.html" rel="nofollow">original article</a>.  Since it is designed as an actual fluent interface, each method always returns the object handle and you don&#8217;t have these problems.</p>
<p>Both of your examples above use object chaining but neither is a fluent interface.  It&#8217;s important to understand the difference and not abuse chaining.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick</title>
		<link>http://mikenaberezny.com/2005/12/20/fluent-interfaces-in-php/#comment-54333</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Wed, 17 Oct 2007 00:06:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikenaberezny.com/?p=35#comment-54333</guid>
		<description>Hi Mike,

Hope you see this, given that I'm commenting on a 2-year-old article, but since this page is the first Google hit for "PHP fluent interfaces" I've got a question-

Is there a workaround for when a method in the middle of a fluent call returns null, or a non-object, or something else unexpected?  For instance, the SimpleXML API tries to expose a fluent interface, but it'll throw you a fatal error if you try to access nodes that don't exist, then call methods on them.  Like
foo-&#62;bar-&#62;baz-&#62;children();
?&#62;
if there is no  node.

Or maybe you have an ORM where an Order has Items, and an Item may or may not have a shipping record with a shipping date, you might do this:
getItem(0)-&#62;getShippingRecord()-&#62;date;
?&#62;
which would fail miserably if the Item has no shipping record.

I feel like usability would improve if my program would just return null in these cases.  Are you aware of anything that would facilitate this?</description>
		<content:encoded><![CDATA[<p>Hi Mike,</p>
<p>Hope you see this, given that I&#8217;m commenting on a 2-year-old article, but since this page is the first Google hit for &#8220;PHP fluent interfaces&#8221; I&#8217;ve got a question-</p>
<p>Is there a workaround for when a method in the middle of a fluent call returns null, or a non-object, or something else unexpected?  For instance, the SimpleXML API tries to expose a fluent interface, but it&#8217;ll throw you a fatal error if you try to access nodes that don&#8217;t exist, then call methods on them.  Like<br />
foo-&gt;bar-&gt;baz-&gt;children();<br />
?&gt;<br />
if there is no  node.</p>
<p>Or maybe you have an ORM where an Order has Items, and an Item may or may not have a shipping record with a shipping date, you might do this:<br />
getItem(0)-&gt;getShippingRecord()-&gt;date;<br />
?&gt;<br />
which would fail miserably if the Item has no shipping record.</p>
<p>I feel like usability would improve if my program would just return null in these cases.  Are you aware of anything that would facilitate this?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: FGM</title>
		<link>http://mikenaberezny.com/2005/12/20/fluent-interfaces-in-php/#comment-16260</link>
		<dc:creator>FGM</dc:creator>
		<pubDate>Sat, 04 Nov 2006 09:57:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikenaberezny.com/?p=35#comment-16260</guid>
		<description>There's still something unclear with this example: in the traditional style, skippable() clearly applies to $line2.

However, in the fluent style, you apply it to the result of with(5, ‘HPK’), which is supposed to be $this representing the customer object, meaning the method skippable() is actually applied to the customer, not to the OrderLine to which it is appended.

Now, if the methods do not return $this, but maybe the created object (OrderLine in this case), AND if the the created object inherits from the original object (Customer) or contains a back reference to it, skippable() can be defined as a method of the created OrderLine and still return the original Customer for the next call to with(), but now, just suppose the OrderLine also needs another optional method like skippable() : how will this one obtain the OrderLine object ? This introduces limitations that seem unexpected initially.

Of course, in any case, methods could store additional information in the original object so methods like skippable() could fetch the OrderLine info from the Customer object, but this means that the class decoupling has been removed: methods for OrderLine now need knowledge about Customer, which they shouldn't be needing in the first place (law of Demeter ?).

Or maybe I missed something ?</description>
		<content:encoded><![CDATA[<p>There&#8217;s still something unclear with this example: in the traditional style, skippable() clearly applies to $line2.</p>
<p>However, in the fluent style, you apply it to the result of with(5, ‘HPK’), which is supposed to be $this representing the customer object, meaning the method skippable() is actually applied to the customer, not to the OrderLine to which it is appended.</p>
<p>Now, if the methods do not return $this, but maybe the created object (OrderLine in this case), AND if the the created object inherits from the original object (Customer) or contains a back reference to it, skippable() can be defined as a method of the created OrderLine and still return the original Customer for the next call to with(), but now, just suppose the OrderLine also needs another optional method like skippable() : how will this one obtain the OrderLine object ? This introduces limitations that seem unexpected initially.</p>
<p>Of course, in any case, methods could store additional information in the original object so methods like skippable() could fetch the OrderLine info from the Customer object, but this means that the class decoupling has been removed: methods for OrderLine now need knowledge about Customer, which they shouldn&#8217;t be needing in the first place (law of Demeter ?).</p>
<p>Or maybe I missed something ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anon</title>
		<link>http://mikenaberezny.com/2005/12/20/fluent-interfaces-in-php/#comment-328</link>
		<dc:creator>Anon</dc:creator>
		<pubDate>Sat, 11 Mar 2006 04:37:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikenaberezny.com/?p=35#comment-328</guid>
		<description>hello,

&#62; It is perhaps simplest to always return $this, however any object
&#62; handle can be returned.

looking at the zend framework, i note that there are places in the controller package, which return $this, so i have to think that is why you have done this, in regards to fluent interfaces?</description>
		<content:encoded><![CDATA[<p>hello,</p>
<p>&gt; It is perhaps simplest to always return $this, however any object<br />
&gt; handle can be returned.</p>
<p>looking at the zend framework, i note that there are places in the controller package, which return $this, so i have to think that is why you have done this, in regards to fluent interfaces?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Herbert AraujoSeção de dados do blog &#187; Fluent Interfaces no PHP 5</title>
		<link>http://mikenaberezny.com/2005/12/20/fluent-interfaces-in-php/#comment-312</link>
		<dc:creator>Herbert AraujoSeção de dados do blog &#187; Fluent Interfaces no PHP 5</dc:creator>
		<pubDate>Fri, 24 Feb 2006 22:35:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikenaberezny.com/?p=35#comment-312</guid>
		<description>[...]  uma explicação mais detalhada da técnica acesse &#8220;FluentInterface&#8221; e &#8220;Fluent Interfaces in PHP&#8220;.   				 				 					 					Este conteúdo foi publicado em Sexta, 24 de Fev [...]</description>
		<content:encoded><![CDATA[<p>[...]  uma explicação mais detalhada da técnica acesse &#8220;FluentInterface&#8221; e &#8220;Fluent Interfaces in PHP&#8220;.   				 				 					 					Este conteúdo foi publicado em Sexta, 24 de Fev [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.581 seconds -->
<!-- Cached page served by WP-Cache -->
