<?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>amptools &#187; type boxing</title>
	<atom:link href="http://www.amptools.net/tag/type-boxing/feed" rel="self" type="application/rss+xml" />
	<link>http://www.amptools.net</link>
	<description>Simplify your life.</description>
	<lastBuildDate>Sat, 08 May 2010 07:40:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Boxing and Unboxing in PHP</title>
		<link>http://www.amptools.net/blog/boxing-and-unboxing-in-php</link>
		<comments>http://www.amptools.net/blog/boxing-and-unboxing-in-php#comments</comments>
		<pubDate>Tue, 16 Jun 2009 18:06:48 +0000</pubDate>
		<dc:creator>Michael Herndon</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Boxing]]></category>
		<category><![CDATA[Chaining]]></category>
		<category><![CDATA[midori-php]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[type boxing]]></category>
		<category><![CDATA[unboxing]]></category>

		<guid isPermaLink="false">http://www.amptools.net/?p=102</guid>
		<description><![CDATA[If you are not familiar with type/value boxing using objects, head over to wikipedia&#8217;s object_type article to grasp the concept. While languages such as Java and C# support this natively, PHP does not. However it can be accomplished, to some degree in PHP, just not eloquently as the language itself does not have constructs to [...]]]></description>
			<content:encoded><![CDATA[<img class='gravatar' style='float: left; margin-right: 10px; border: none;' src='http://www.gravatar.com/avatar.php?gravatar_id=899a978b0f02774a965aec11eeb0b8f4&amp;default=http://amppotls.net' alt='No Gravatar' width=48 height=48/><p>If you are not familiar with type/value boxing using objects, head over to <a href="http://en.wikipedia.org/wiki/Object_type" rel="tag">wikipedia&#8217;s object_type article</a> to grasp the concept. While languages such as Java and C# support this natively, PHP does not. However it can be accomplished, to some degree in PHP, just not eloquently as the language itself does not have constructs to support it.&#160; Having box types in PHP could help with chaining, keeping everything object oriented and allows for type hinting in method signatures.&#160; The downside is overhead and the fact that you now have extra checking to do using the â€œinstanceofâ€ construct.&#160; Having a type system is also a plus when using development tools that have intellisense or code assist like PDT. Rather than having to google/bing/yahoo for the method, it exists on the object, and you can use the tool to provide a drop down.</p>
<h3> Boxing and Unboxing with Midori PHP </h3>
<p>In <a href="/projects/midori-php/" rel="tag">Midori PHP</a> Iâ€™ve add box types for native values in PHP. Granted not all the types are there yet, as iâ€™m slowly porting them in, writing documentation, writing tests, and refactoring what currently exists from the prototype.&#160; The key classes to look at would be the <a href="http://github.com/michaelherndon/midori-php/blob/9e83502e28cbc278ec6bb4a293331dce9fa9dc7f/src/Midori/Object.php" rel="tag">Midori_Object</a>, <a href="http://github.com/michaelherndon/midori-php/blob/9e83502e28cbc278ec6bb4a293331dce9fa9dc7f/src/Midori/Nullable.php" rel="tag">Midori_Nullable</a>, and <a href="http://github.com/michaelherndon/midori-php/blob/9e83502e28cbc278ec6bb4a293331dce9fa9dc7f/src/Midori/String.php" rel="tag">Midori_String</a>.&#160; The Midori_Nullable class currently abstracts a nullable value into a class which is built on the Midori_Object and is the parent of Midori_String.&#160; So using these box classes might look like the follow:&#160; </p>
<h4>PHP</h4>
<pre class="brush: php;">

	// boxing example
	$str = new Midori_String(&quot;my cool new value&quot;); // value is now boxed
	$str2 = $str-&gt;replace(&quot;value&quot;, &quot;test&quot;);

	// unboxing
	$unboxedStr = $str2-&gt;value; // unboxed value

	echo $unboxedStr; // my cool new test
	echo $str // my cool new value
	echo $str2 // my cool new test
</pre>
<p>
Midori_String uses the magic method __toString to allow echo to called on the object and the Midori_String will even allow for dot concatination since it has __toString.&#160;  The &#8220;value&#8221; property is the actual value inside of the Midori_String box object to which it wraps. The Midori_String also returns the new value and does not affect the original value, during each method call, so that actual object acts just like a value type.
</p>
<h3> Understanding Chaining Methods after Instantiation </h3>
<p>
Another problem in PHP is that when you instantiate an object,  it does not allow for chaining right away, which can be bothersome.
</p>
<h4>PHP</h4>
<pre class="brush: php;">

	// incorrect way to chain in PHP
	$str = new Midori_String(&quot;don't objectify me, sidekick&quot;)-&gt;concat(&quot;!&quot;); // epoch fail

	// correct way to chain
	$str = new Midori_String(&quot;don't objectify me, sidekick&quot;);
	echo $str-&gt;concat(&quot;!&quot;); // don't objectify me, sidekick!
</pre>
<p>
 So in hopes of getting around that and to use common box types often: I propose creating and using functions &#8220;box_x&#8221; to wrap the instantiation of a box type and to allow for chaining.&#160;  I also thinking having an &#8220;unbox&#8221; function to allow for unboxing of values if the object is an instance of Midori_Nullable, would also be handy.
</p>
<h4>PHP</h4>
<pre class="brush: php;">

	// boxing example with forth coming helper methods
	// these methods are prototypes and would like feed back.

	$str = box_str(&quot;my cool new value&quot;)
		-&gt;replace(&quot;value&quot;, &quot;test&quot;)	//chain 1, replacing a value
		-&gt;toUpper()			// chain 2, uppercasing.
		-&gt;value;				// chain 3, returning the unboxed value

	echo $str; // MY COOL NEW TEST 

 	$str = box_str(&quot;my cool new value!&quot;)-&gt;trimEnd(&quot;!&quot;);
	$unboxed = unbox($str);

	echo $unboxed; // my cool new value!
</pre>
<h3>PHP does not allow for operator operations on objects</h3>
<p> Unfortunately, <a href="http://www.amptools.net/blog/php-lacks-true-object-comparability">PHP does not allow for operator overload on objects like Ruby, C#, and other languages do</a>, so you will have to unbox objects in order to do math routines or apply conditional statements to them. </p>
<pre class="brush: php;">

	// example of what you have to do to add two box objects in PHP
	$grade = new Midori_Int32(90);
	$grade2 = new Midori_Int32(60);

	// correct way to add 2 boxed objects in PHP using Midori
	$sum = unbox($grade) + unbox(grade2);
	$sum = $grade-&gt;value + $grad2-&gt;value;
	echo $sum; // 150

	// incorrect way, though it would be nice if PHP could support it
	$sum = $grade + $grade;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.amptools.net/blog/boxing-and-unboxing-in-php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->