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

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


Generally speaking, you should always use arrays unless there is a good reason to use objects. And the only good reason to use objects over arrays in PHP is if you need some sort of functionality for your data that arrays do not provide (such as private / protected variables, methods, object magic, defined structure, etc). If you're dealing strictly with data, array format is better because you can easily manipulate your data using PHP's vast array of array functions (no pun intended).

UPDATE:

If all you are doing is passing data between two points, arrays are much more suited for that purpose. The only reason (as far as I can tell) that one would ever use an object over an array as a mere container for data is if the receiving side needed a guaranteed format (think interfaces, etc).

From what I can tell, you should be returning arrays from CouchDB to avoid having to convert altogether.


It depends on the paradigm you want to work in. Fundamentally your data is all key-value pairs stored in CouchDB. Maybe it can be easily mapped to objects/entities and you are more comfortable thinking of it in terms of objects that have properties and methods. Or, maybe not and maybe you are more comfortable working with the raw data without the abstraction that classes provide. I think this is the key distinction that should inform your decision.

There is not a lot of overhead involved with objects vs. arrays in PHP. Internally they are treated similarly by the PHP engine. You might want to benchmark it yourself if it's a concern, but I think that you won't find a lot of difference unless you are pushing massive traffic or have a very constrained server. Obviously your web app would be faster if you wrote it in optimized x86 assembler, but you don't do that because you want to be able to enjoy the convenience that a high-level language like PHP provides. So execute the best design now, and optimize later, if it's even necessary.