Amptools.Net

simplify your life.

type boxing

Boxing and Unboxing in PHP

No Gravatar

If you are not familiar with type/value boxing using objects, head over to 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.  Having box types in PHP could help with chaining, keeping everything object oriented and allows for type hinting in method signatures.  The downside is overhead and the fact that you now have extra checking to do using the “instanceof” construct.  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.

Boxing and Unboxing with Midori PHP

In 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.  The key classes to look at would be the , , and .  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.  So using these box classes might look like the follow: 

PHP


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

	// unboxing
	$unboxedStr = $str2->value; // unboxed value

	echo $unboxedStr; // my cool new test
	echo $str // my cool new value
	echo $str2 // my cool new test

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.  The “value” 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.

Understanding Chaining Methods after Instantiation

Another problem in PHP is that when you instantiate an object, it does not allow for chaining right away, which can be bothersome.

PHP


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

	// correct way to chain
	$str = new Midori_String("don't objectify me, sidekick");
	echo $str->concat("!"); // don't objectify me, sidekick!

So in hopes of getting around that and to use common box types often: I propose creating and using functions “box_x” to wrap the instantiation of a box type and to allow for chaining.  I also thinking having an “unbox” function to allow for unboxing of values if the object is an instance of Midori_Nullable, would also be handy.

PHP


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

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

	echo $str; // MY COOL NEW TEST 

 	$str = box_str("my cool new value!")->trimEnd("!");
	$unboxed = unbox($str);

	echo $unboxed; // my cool new value!

PHP does not allow for operator operations on objects

Unfortunately, PHP does not allow for operator overload on objects like Ruby, C#, and other languages do, so you will have to unbox objects in order to do math routines or apply conditional statements to them.


	// 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->value + $grad2->value;
	echo $sum; // 150

	// incorrect way, though it would be nice if PHP could support it
	$sum = $grade + $grade;

Tags: , , , , ,

Tuesday, June 16th, 2009 Blog Comments