PHP Format abstraction with a simple class
Having a quiet night in before a long day of kayaking I thought I'd write some fun code instead of banging out the usual shit like I've been doing all day...
Convert between Array, Object, JSON, XML, CSV and Serialized data and back again easily. I'll add a few more types like YAML when I can be arsed to work out PECL for MAMP.
$original_array = array('foo' => 'bar', 'baz' => 'stuff');
$json = Format::factory($original_array)->to_json();
$xml = Format::factory($json, 'json')->to_xml();
$end_array = Format::factory($xml, 'xml')->to_array();
var_dump($original_array === $end_array); // true
Get it on GitHub.
I've put it into FuelPHP and I'll commit to CodeIgntier 2.0 soon.

Comments
Richard
2011-02-12
Rad! Thanks for the code, I can certainly see me using this. All this 5.3 stuff is void for CI2 though, right? (late static binding etc).
Stu
2011-02-13
I like it, nice one Phil!
Phil Sturgeon
2011-02-16
You can just change the static:: to self::, that was just a convention we use in Fuel.
Joel Kallman
2011-02-17
Phil,
What are the benefits (outside syntax) of this line of code (29): return new static($data, $from_type);
Over using the usual CI method of
$this->load->libary('factory', array('data' => $data, 'from_type' => $from_type);
$this->factory->to_xml();
What are the benefits of using this technique?
Shaunitwec
2011-03-25
HelloGuys!
Nice to be here. Check this out for PHP Programmers.
Providing some special features in php web development.