UPDATE
MJ7′s post, below, reminded me of the existence of this long forgotten post.
As I had written in the comments, this tongue-in-cheek post was not meant to be taken seriously. Please, do not do this.
JSON, or JavaScript Object Notation, is fairly popular.
This is just my modest take on a ‘PHP Object Notation’ implementation.
Note that it is meant to follow how your mind works rather than being syntactically correct; therefore, associative arrays are used in a loose manner where you have to worry about associative syntax as little as possible.
Basically, type away until your object is fully described.
Here is a notation sample:
.
This syntax can then be used the same way JSON is used, using eval. Here is how and you will immediately notice how the eval() syntax has to differ from the JavaScript one:
$remote_str = <<<EOB array( 'root level' => array( 'sublevel 1' => array( 'sublevel 2' => 'sublevel 3', ), ) ); EOB; // ... eval('$local_str='.$remote_str.';'); |
.
Feels a bit clumsy. Fortunately, starting with PHP4, if you are not too sad about introducing a statement in your remote string, you can rewrite it like this:
$remote_str = <<<EOB return array( 'root level' => array( 'sublevel 1' => array( 'sublevel 2' => 'sublevel 3', ), ) ); EOB; // ... $local_str = eval($remote_str); |
.
Better, isn’t it?
In this last listing, you will find the very simple code I wrote to traverse the object. Note that I invoke a callback for every element, otherwise this code would not be very helpful.
Note that this is just a concept for something I needed to throw together quickly therefore there are some limitations; for instance, you cannot have numeric keys since in this context their “keyness” is ignored and even feared.
<?php function traverse_tree($callback, $curlevel, $curdepth, $uid) { $depthstr = '------------------------------------------------------------------------------'; $padding = substr($depthstr, 0, $curdepth); if(!is_array($curlevel)) { $callback($curlevel, $padding, $uid); return; } foreach($curlevel as $level=>$children) { if(!is_numeric($level)) { $my_uid = $callback($level, $padding, $uid); traverse_tree($callback, $children, $curdepth + 2, $my_uid); } else { $callback($children, $padding, $uid); } } } function do_something($name, $padding, $uid) { $iid = 0; // Bogus value print $padding.$name." ($iid)n"; return $iid; } // ----------------------------------------------------------- // Here we go // ----------------------------------------------------------- if(count($argv) != 2) die("nSyntax: ".$PHP_SELF." 'root_node_name'nn"); $rootnodename = &$argv[1]; $INS = array( $rootnodename => array( 'Bikes' => array( 'Yamaha' => 'Fz', 'Honda', 'Norton' ), 'Trikes', 'Cars' => array( 'Chevrolet' => array( 'Corvette' => array('Z06', 'Convertible', 'Coupe'), 'Camaro', 'Jimmy' ), 'Ford' => array( 'Mustang' => array('GT', 'Premium', 'Shelby', 'Other'=>'Roush'), 'Explorer', 'Crown Victoria' ), 'Chrysler' => array( 'Dodge' => 'Charger', '300X', 'Le Baron' ), ) ) ); traverse_tree(do_something, $INS, 0, 1); print "DONEnn"; ?> |
.
Invoke with:
php my_php_script.php "Vehicles" |
.
“What is this $iid variable?” you may ask.
You will notice that this piece of code is particularly convenient if you want to add a whole tree structure to your database. Say the you are using MySQL and want to create categories and sub-categories, using the column ‘pid’ as a category’s parent id. You would rewrite do_something:
function do_something($name, $padding, $uid) { mysql_query("INSERT INTO my_cat_table(pid, title) VALUES({$uid}, '{$name}')"); $iid = mysql_insert_id(); print $padding.$name." ($iid)n"; return $iid; } |
Voila!







thats it, guy
Well, that article was written mostly in jest — do not give it too much credit, I was just blowing some steam as I had spent the previous week working exclusively with SOAP
Obviously if I were to use something like this, it would be between two trusted servers, not for web clients; but I wouldn’t even use this guy as there are other existing lightweight solutions that fit the bill, such as JSON (indeed) or YAML. Even XML-RPC is lightweight compared to a full-on SOAP exchange.
Again, after spending a week with SOAP, this was just a — liberating — exercise in finding an implementation that would be its exact opposite: as little overhead as possible
Wouldn’t waiting for PHP 5.3 to become a stable release and doing Lambda functions (anonymous functions) be better than doing eval? From the examples I have looked over, it seems we could do the following. I may be completely wrong on this, since I don’t have a 5.3 RC installed.
function($x)
{
return($x*10);
});
echo $arr['somFunction'](5);
With closures you could do more with the ‘use’ keyword.
$arr = array(‘val1′ => 0,
‘val2′ => 0′,
‘func’ => function()use(&$arr)
{
return($arr['val1']*$arr['val2']);
});
$arr['val1] = 5;
$arr['val2] = 10;
echo $arr['func']();
Like I said, I could be completely wrong and/or there could be a better way, like using a keyword would that would represent (this or self) to reference the array.
@EllisGL –
I do not know if your syntax works with 5.3 but it sure makes sense, obviously.
Note that, as mentioned earlier, I had written this piece mostly tongue-in-cheek because SOAP was slowly killing me inside!
@admin – I completely understand.
I appreciate you wrote this as a tongue-in-cheek post but just in case someone stumbles across this and is seriously looking into passing PHP objects via AJAX or just as strings (stored in files on in DB etc) I would recommend PHP’s serialize/unserialize functions – http://php.net/manual/en/function.serialize.php
It is very quick and easy to use, very efficient/robust and also means that you do not need to use eval() which is a security hole.
Anyway – just a little note in case someone is interested.
Some good stuff on this site though, thanks & keep it up.