<?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>plasmid.co.uk &#187; toString</title>
	<atom:link href="http://plasmid.co.uk/tag/tostring/feed/" rel="self" type="application/rss+xml" />
	<link>http://plasmid.co.uk</link>
	<description>because life is faster in reverse ...</description>
	<lastBuildDate>Thu, 20 May 2010 23:07:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<image>
  <link>http://plasmid.co.uk</link>
  <url>http://plasmid.co.uk/wp/favicon.ico</url>
  <title>plasmid.co.uk</title>
</image>
		<item>
		<title>PHP 5.2.x __toString() Support</title>
		<link>http://plasmid.co.uk/2008/07/28/php-52x-__tostring-support/</link>
		<comments>http://plasmid.co.uk/2008/07/28/php-52x-__tostring-support/#comments</comments>
		<pubDate>Mon, 28 Jul 2008 17:52:16 +0000</pubDate>
		<dc:creator>plasmid</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[toString]]></category>

		<guid isPermaLink="false">http://plasmid.co.uk/?p=41</guid>
		<description><![CDATA[Since updating to PHP version 5.2.2 (which has been superseded yet again since this post) the default behavior of class objects is to raise an error when cast to a string (usually a 'Catchable Fatal Error', citing that the object could not be converted to string!).
This is quite irritating, as previously the default behavior would [...]]]></description>
			<content:encoded><![CDATA[<p>Since updating to PHP version 5.2.2 (which has been superseded yet again since this post) the default behavior of class objects is to <strong>raise an error when cast to a string</strong> (usually a 'Catchable Fatal Error', citing that the object could not be converted to string!).</p>
<p>This is quite irritating, as previously the default behavior would be to <strong>return a unique object identifier</strong>. However, there is a quick workaround for this problem should your script rely upon object string casting / this unique object identifier.</p>
<p><strong>The Fix</strong></p>
<p>When creating your class, add in __toString() as a function. This so-called '<em>magic function</em>' is called when trying to cast an object to string, such as <code>(string) $someObject</code>. The following combines this function with a call to <strong>spl_object_hash()</strong>, which provides an identifier hash for the object:</p>
<pre lang="code" class="php">
&lt;?php

class SomeClass {
	function __toString() {
		return spl_object_hash($this);
	}
}

$someObject = new SomeClass;
echo $someObject;

?&gt;
</pre>
<p>The above code is fairly self-explanatory and it saves you from at least a few of minutes banging your head on the table :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://plasmid.co.uk/2008/07/28/php-52x-__tostring-support/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.438 seconds -->
