Amptools.Net

simplify your life.

midori-php

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

Fun Times with Partial Loops in Zend Framework

No Gravatar

While working with the Zend Framework and partial loops, I ran into a few issues. One was not getting the data to load, which was easily solved after looking at . Make sure you set the object key in the controller for the partial loop. The other issue of trying to pass local variables into the partial loop took time to see there was really no current answer without extending or creating a new helper, or the evil global.

PHP

// in the action method
global $otherVariable;
$otherVariable = "should be the same through each iteration";
$this->view->partialLoop()->setObjectKey('model');
$this->view->paginator = Zend_Paginator::factory($array); //etc

// in the view
<?= $this->partialLoop('path/to/_partial.phtml', $this->paginator) ?>

// in the _partial.phtml
<?php
      $model = $this->model
      global $otherVariable; // just seems so hackish to have to do this.
?>

I have already added a task to to extend the Partial Loop Helper to overcome the lack of the ability to pass in local variables to the partial loop.

Tags: , ,

Thursday, June 11th, 2009 Blog Comments

midori php

No Gravatar

Midori-PHP is the PHP version of the Midori Generative Framework.  Its aim at providing building blocks and springboarding into creating an application.  

Midori-Php was created in order to be used with the Zend Framework initially to bring in some rails like concepts as well as help generate the data layer with a type system.  PHP unfortunately is not a pure object oriented language and lacks core classes similar to what ruby and many other popular languages have to day.  On top of that problem what types PHP’s SPL does provide is surely lacking in both power and simple api, instead conforming to keep the terrible naming convention of its functions.   

Source Code

Tags: ,

Tuesday, May 12th, 2009 Wiki Comments