Should I go for Arrays or Objects in PHP in a CouchDB/Ajax app?

Posted by karlthorwald on Stack Overflow See other posts from Stack Overflow or by karlthorwald
Published on 2010-05-27T17:34:19Z Indexed on 2010/05/30 23:52 UTC
Read the original article Hit count: 297

Filed under:
|
|
|
|

I find myself converting between array and object all the time in PHP application that uses couchDB and Ajax. Of course I am also converting objects to JSON and back (for sometimes couchdb but mostly Ajax), but this is not so much disturbing my workflow.

At the present I have php objects that are returned by the CouchDB modules I use and on the other hand I have the old habbit to return arrays like array("error"=>"not found","data"=>$dataObj) from my functions. This leads to a mixed occurence of real php objects and nested arrays and I cast with (object) or (array) if necessary. The worst thing is that I know more or less by heart what a function returns, but not what type (array or object), so I often run into type errors.

My plan is now to always cast arrays to objects before returning from a function. Of course this implies a lot of refactoring.

Is this the right way to go? What about the conversion overhead? Other ideas or tips?

Edit: Kenaniah's answer suggests I should go the other way, this would mean I'd cast everything to arrays. And for all the Ajax / JSON stuff and also for CouchDB I would use

$myarray = json_decode($json_data,$assoc = true); //EDIT: changed to true, whcih is what I really meant

Even more work to change all the CouchDB and Ajax functions but in the end I have better code.

© Stack Overflow or respective owner

Related posts about php

Related posts about AJAX