<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>PHPDeveloper.org</title>
    <link>http://www.phpdeveloper.org</link>
    <description>Up-to-the Minute PHP News, views and community</description>
    <language>en-us</language>
    <pubDate>Sun, 19 May 2013 06:02:42 -0500</pubDate>
    <ttl>30</ttl>
    <item>
      <title><![CDATA[Larry Garfield: On empty return values]]></title>
      <guid>http://www.phpdeveloper.org/news/19381</guid>
      <link>http://www.phpdeveloper.org/news/19381</link>
      <description><![CDATA[<p>
<i>Larry Garfield</i> has <a href="http://www.garfieldtech.com/blog/empty-return-values">posted some of his thoughts on return values</a> and reminds you about consistent return types, regardless of the result.
</p>
<blockquote>
Earlier today, I posted a brief tweet (isn't that redundant?) about return values in PHP (or really, any language). Originally it was about return values from functions (such an exciting topic, I know), but it ended up generating a fair bit of lively conversation, as well as a <a href="http://drupal.org/node/1927884">patch against Drupal 8</a>. So lively, in fact, that I think it deserves more than 140 characters.
</blockquote>
<p>
He proposes a new rule of thumb: "If your function returns a collection, its null value return must also be a collection." A more broad version of this might be: "make your return types consistent." It's all about predictability and the contracts you have between different parts of your code. If a user calls your method expecting to be able to loop over the results, they'll be disappointed with a "false". He talks some about using and throwing exceptions more effectively for error handling and answers several "but wait..." arguments for his return strategy.
</p>]]></description>
      <pubDate>Fri, 29 Mar 2013 09:15:59 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Brandon Savage: Always Return Something]]></title>
      <guid>http://www.phpdeveloper.org/news/19301</guid>
      <link>http://www.phpdeveloper.org/news/19301</link>
      <description><![CDATA[<p>
In <a href="http://www.brandonsavage.net/always-return-something">this post</a> to his site <i>Brandon Savage</i> talks about "always returning something" from your methods and functions back to the calling script. He also suggests that null is not an option.
</p>
<blockquote>
A few weeks ago, there was a discussion on Twitter about whether or not a method should always return a value, or whether or not null was a valid value to return. The answer to this question is a resounding no, a null value should never be returned. [...] For example, you check that a file you opened exists, or that a resource performed correctly before using it. But if you receive a null response, how do you test for this The answer is you can't
</blockquote>
<p>
He notes that a "null" response is not only difficult to test but can lead to ambiguous handling as you're not sure where the error might be. He also includes a snippet of code showing how a null response could break a fluent interface if an instance of "$this" is not returned.
</p>]]></description>
      <pubDate>Tue, 12 Mar 2013 10:49:55 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Michael Nitschinger: A Journey on Avoiding Nulls in PHP]]></title>
      <guid>http://www.phpdeveloper.org/news/19210</guid>
      <link>http://www.phpdeveloper.org/news/19210</link>
      <description><![CDATA[<p>
<i>Michael Nitschinger</i> has written up a post looking at <a href="http://nitschinger.at/A-Journey-on-Avoiding-Nulls-in-PHP">avoiding nulls in your applications</a> in favor of a better kind of value handling - the introduction of "<a href="http://code.google.com/p/guava-libraries/wiki/UsingAndAvoidingNullExplained">Optional</a>" handling.
</p>
<blockquote>
While every developer has kind of accepted their existence, they are suddenly there when we'd desperately need them to not show up. How often did you writeif($obj === null) in your PHP code? Can't there be a better, more elegant and fault-tolerant solution to the problem?
</blockquote>
<p>
His solution is to create a PHP version of this "Optional" functionality (via an abstract class) that allows some evaluation of the returned value from method calls on the object. Methods like "isPresent", "getOrElse", "of" and "fromNullable"  make it easier to work with null values instead of just the triple-equals checking. He includes not only the code for the classes you'll need to implement it but examples of it in use - an "Optional" abstract class and two child classes, "Present" and "Absent".
</p>]]></description>
      <pubDate>Wed, 20 Feb 2013 12:17:39 -0600</pubDate>
    </item>
    <item>
      <title><![CDATA[Joshua Thijssen: PHP5.5: Try/Catch/Finally]]></title>
      <guid>http://www.phpdeveloper.org/news/19169</guid>
      <link>http://www.phpdeveloper.org/news/19169</link>
      <description><![CDATA[<p>
<i>Joshua Thjissen</i> has a new post to his site today about a feature that's been introduced in the upcoming PHP 5.5 release of the language - the addition of "finally" to try/catch exception handling. He gets into it <a href="http://www.adayinthelifeof.nl/2013/02/12/php5-5-trycatchfinally/">a bit more technically</a> than just the "introductory" level, discussing parent/child exception handling and using returns.
</p>
<blockquote>
Exception handling is available in PHP since version 5.  It allows you to have a more fine-grained control over code when things go wrong ie, when exceptions occur. But since PHP 5.5, exception handling has finally evolved into what it should have been from the beginning: the "finally" part has been implemented.
</blockquote>
<p>
He includes a basic example showing how a certain part is always executed, regardless of if the exception is thrown or not. He also shows how a "chained catch" would work to catch multiple kinds of exceptions and when the "finally" is run as it relates to the "trickle down" handling of exceptions. He then gets a little more complex and introduces "return" into the mix. Of special note, even if you return, the "finally" will still get called.
</p>]]></description>
      <pubDate>Tue, 12 Feb 2013 10:03:23 -0600</pubDate>
    </item>
    <item>
      <title><![CDATA[Rob Allen's Blog: Returning JSON using the Accept header in ZF2]]></title>
      <guid>http://www.phpdeveloper.org/news/17756</guid>
      <link>http://www.phpdeveloper.org/news/17756</link>
      <description><![CDATA[<p>
In a <a href="http://phpdeveloper.org/news/17748">previous post</a> <i>Rob Allen</i> showed how to return JSON data from a controller in a Zend Framework 2 application. In <a href="http://akrabat.com/zend-framework-2/returning-json-using-the-accept-header-in-zf2/">this new post</a> he shows how to use the "Accepts" header from the client to do the same thing.
</p>
<blockquote>
Following yesterday's article on <a href="http://akrabat.com/zend-framework-2/returning-json-from-a-zf2-controller-action/">returning JSON from a ZF2 controller action</a>, <a href="http://twitter.com/lsmith">Lukas</a> suggested that I should also demonstrate how to use the Accept header to get JSON. So this is how you do it!
</blockquote>
<p>
You'll need to <a href="http://akrabat.com/zend-framework-2/returning-json-from-a-zf2-controller-action/">create the JsonStrategy</a> first, then you can return the ViewModel from the controller. If all goes well, you should see the sample HTML page rendered in a browser and JSON output when requested with the right "Accept" header (he uses curl in his example).
</p>]]></description>
      <pubDate>Fri, 30 Mar 2012 08:01:56 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Rob Allen's Blog: Returning JSON from a ZF2 controller action]]></title>
      <guid>http://www.phpdeveloper.org/news/17748</guid>
      <link>http://www.phpdeveloper.org/news/17748</link>
      <description><![CDATA[<p>
In a new post to his blog <i>Rob Allen</i> shows how you can return JSON data <a href="http://akrabat.com/zend-framework-2/returning-json-from-a-zf2-controller-action/">directly back from a controller</a> in a Zend Framework 2 application.
</p>
<blockquote>
The new view layer in Zend Framework 2 can be set up to return JSON rather than rendered HTML relatively easily. [...] Firstly we need to set up the view's JsonStrategy to check to a situation when returning JSON is required and then to render out JSON for us.
</blockquote>
<p>
This "JsonStrategy" does some of the hard work for you - detecting when the client is requesting a JSON response and looking at the data coming into the view to see if it's JSON. He shows how to implement it in a sample module using the "onBootstrap" module and how to force a return of the JsonModel even when JSON isn't requested (useful for a consistent interface).
</p>]]></description>
      <pubDate>Thu, 29 Mar 2012 08:55:39 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Timothy Boronczyk's Blog: Avoid Fetch-Object Abuse]]></title>
      <guid>http://www.phpdeveloper.org/news/16581</guid>
      <link>http://www.phpdeveloper.org/news/16581</link>
      <description><![CDATA[<p>
In a new post on his blog <i>Timothy Boronczyk</i> has a recommendation for developers working in PHP with databases that have a "fetch object" method - <a href="http://zaemis.blogspot.com/2011/07/avoid-fetch-object-abuse.html">use it correctly or avoid it all together</a>.
</p>
<blockquote>
Lately I'm finding a lot of instances of the mysql_fetch_object() function being used in a particular codebase I help maintain. Unfortunately, I've yet to see it used correctly. It always seems to be used to retrieve a stdClass object from a query result where mysql_fetch_array() or mysql_fetch_assoc() would be the more appropriate choice.
</blockquote>
<p>
Most of his complaint is that, despite pulling out the data as an object, most scripts continue to use it like you would an array, looping over it. There's extra overhead generated from the object creation that could cause issues, especially with large return data sets.
</p>
<blockquote>
The best advice I can offer is to educate yourself and others how the function should be used so its abuse isn't perpetuated. Then, be cautious when using mysql_fetch_object() correctly and understand the process it follows to create and return an object. If not for yourself, then do it for the kittens.
</blockquote>]]></description>
      <pubDate>Tue, 12 Jul 2011 12:10:02 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Lorna Mitchell's Blog: PHP Returning Numeric Values in JSON]]></title>
      <guid>http://www.phpdeveloper.org/news/16577</guid>
      <link>http://www.phpdeveloper.org/news/16577</link>
      <description><![CDATA[<p>
<i>Lorna Mitchell</i> has a <a href="http://www.lornajane.net/posts/2011/PHP-Returning-Numeric-Values-in-JSON">quick reminder</a> about an issue in the new <a href="http://joind.in/">joind.in</a> API version - everything was being returned as strings, even integers.
</p>
<blockquote>
A few weeks later (my inbox is a black hole and it takes a while to process these things) I fell over a throwaway comment to an undocumented constant JSON_NUMERIC_CHECK, and I added the constant name to my todo list. In the time it took for me to actually get around to googling for this, some wonderful person updated the PHP manual page (this is why I love PHP) to include it as a documented option, and someone else had added a user contributed note about using it.
</blockquote>
<p>
This option, JSON_NUMERIC_CHECK, tells the <a href="http://php.net/json_encode">json_encode</a> function to property consider numbers in its encoding process. It applies globally, so if there's an instance where you don't want something assigned as a number, you might need to go with another, more flexible JSON encoding option. You can find information about this and other possible options json_encode can take on <a href="http://us.php.net/json_encode">its manual page</a>.
</p>
]]></description>
      <pubDate>Tue, 12 Jul 2011 08:41:39 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Dan Horrigan's Blog: The Value of Null]]></title>
      <guid>http://www.phpdeveloper.org/news/16217</guid>
      <link>http://www.phpdeveloper.org/news/16217</link>
      <description><![CDATA[<p>
<i>Dan Horrigan</i> has a new post to his blog talking about <a href="http://coderepeat.com/post/4624154050/the-value-of-null">the value of null</a> - a quick summary about when and where null should be used. Null's a value too, after all...
</p>
<blockquote>
Let me start off by saying this article is about PHP and PHP alone.  Other languages handle this sort of thing differently (and better). In PHP many people (and a few frameworks) return FALSE from methods when the requested value does not exist.  However, I am here to tell you that if you do this, you are doing it wrong.  Plain and Simple.
</blockquote>
<p>
In his opinion, "false" is definitely not the same thing as "null" because "null" is technically the absence of a value, not a "not true" value like "false" is. He illustrates with a simple use case of a class that has methods returning various values. 
</p>]]></description>
      <pubDate>Tue, 19 Apr 2011 10:51:18 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Brian Swan's Blog: SQL Server Driver for PHP Connection Options: ReturnDatesAsStrings]]></title>
      <guid>http://www.phpdeveloper.org/news/15876</guid>
      <link>http://www.phpdeveloper.org/news/15876</link>
      <description><![CDATA[<p>
<i>Brian Swan</i> has a new post to his blog today looking at one of the <a href="http://blogs.msdn.com/b/brian_swan/archive/2011/02/08/sql-server-driver-for-php-connection-options-returndatesasstrings.aspx">connection options for the SQL Server driver</a> in PHP - the "ReturnDatesAsStrings" setting that can make handling date and time information simpler for PHP.
</p>
<blockquote>
This is short post to address a frustration I've seen mentioned on Twitter and in forums a lot: By default, the SQL Server Driver for PHP returns <a href="http://msdn.microsoft.com/en-us/library/ms187819.aspx">datetime</a> columns as <a href="http://www.php.net/manual/en/class.datetime.php">PHP DateTime</a> objects, not strings. This can be especially frustrating if you are not aware of the ReturnDatesAsStrings connection option. By simply setting this option to 1 (or true) when you connect to the server, datetime columns will be returned as strings.
</blockquote>
<p>
He includes some sample code showing how to use the setting (as a part of the settings array passed in to sqlsrv_connect) and the resulting array key from the fetched results on his sample table. This just gives you one more option for handling dates in your SQL Server-based application, especially if you don't need the full DateTIme object's functionality.
</p>]]></description>
      <pubDate>Wed, 09 Feb 2011 08:45:28 -0600</pubDate>
    </item>
  </channel>
</rss>
