Text

SPL has many goodies that help save you time, one of the handiest classes is the ArrayObject class.

Often, array notation looks ugly in code and can be more difficult to manage, particularly in template classes mixing PHP and HTML.

A nice way to get around this is to access information in your array using object notation. For example rather than for example echo $array['my_key']; going echo $array->my_key;

How ArrayObject helps us here is perhaps most simply demonstrated in a code snippet.

$array = array('key1'=>'apples', 'key2'=>'oranges', 'key3'=>'bananas');
$object = new ArrayObject($array);

// now the clever bit
$object->setFlag(ArrayObject::ARRAY_AS_PROPS);
echo $object->key_1;
echo $object->key_2;
echo $object->key_3;

This code is pretty straight forward, you create an associative array with some unique key=>value pairs create an arrayobject setting a flag to treat the array as object properties. Then simply using the key names as properties gives you access to the values i.e. the program will output 'apples', 'oranges' and 'bananas'.

Text

One of the most intimidating aspects of starting out with the Zend Framework isĀ  Bootstrapping. Matthew Weier O'Phinny helps demystify this complex element.

Text

It's really easy with the Zend Framework to transform text from a format where words are separated by dashes or underscores to another format like camelcase.

$inflector = new Zend_Filter_Inflector(':string'); 

$inflector->addRules(array(':string' => array('Word_UnderscoreToCamelCase')));

echo $inflector->filter(array('string'=>$string));

Running this code where $string = 'hello_world' will echo helloWorld.

Simple!

Thanks to a comment below from Camilo, this can be achieved simpler still:

$filter = new Zend_Filter_Word_UnderscoreToCamelCase();
  echo $filter->filter('hello_world');