Why return object instead of array?

These are the reasons why I prefer objects in general:

  • Objects not only contain data but also functionality.
  • Objects have (in most cases) a predefined structure. This is very useful for API design. Furthermore, you can set properties as public, protected, or private.
  • objects better fit object oriented development.
  • In most IDE's auto-completion only works for objects.

Here is something to read:

  • Object Vs. Array in PHP
  • PHP stdClass: Storing Data in an Object Instead of an Array
  • When should I use stdClass and when should I use an array in php5 oo code
  • PHP Objects vs Arrays
  • Mysql results in PHP - arrays or objects?
  • PHP objects vs arrays performance myth
  • A Set of Objects in PHP: Arrays vs. SplObjectStorage
  • Better Object-Oriented Arrays

This probably isn't something you are going to deeply understand until you have worked on a large software project for several years. Many fresh computer science majors will give you an answer with all the right words (encapsulation, functionality with data, and maintainability) but few will really understand why all that stuff is good to have.

Let's run through a few examples.

  • If arrays were returned, then either all of the values need to be computed up front or lots of little values need to be returned with which you can build the more complex values from.

Think about an API method that returns a list of WordPress posts. These posts all have authors, authors have names, e-mail address, maybe even profiles with their biographies.

If you are returning all of the posts in an array, you'll either have to limit yourself to returning an array of post IDs:

[233, 41, 204, 111]

or returning a massive array that looks something like:

[ title: 'somePost', body: 'blah blah', 'author': ['name': 'billy', 'email': '[email protected]', 'profile': ['interests': ['interest1', 'interest2', ...], 'bio': 'info...']] ]
[id: '2', .....]]

The first case of returning a list of IDs isn't very helpful to you because then you need to make an API call for each ID in order to get some information about that post.

The second case will pull way more information than you need 90% of the time and be doing way more work (especially if any of those fields is very complicated to build).

An object on the other hand can provide you with access to all the information you need, but not have actually pulled that information yet. Determining the values of fields can be done lazily (that is, when the value is needed and not beforehand) when using an object.

  • Arrays expose more data and capabilities than intended

Go back to the example of the massive array being returned. Now someone may likely build an application that iterates over each value inside the post array and prints it. If the API is updated to add just one extra element to that post array then the application code is going to break since it will be printing some new field that it probably shouldn't. If the order of items in the post array returned by the API changes, that will break the application code as well. So returning an array creates all sorts of possible dependencies that an object would not create.

  • Functionality

An object can hold information inside of it that will allow it to provide useful functionality to you. A post object, for instance, could be smart enough to return the previous or next posts. An array couldn't ever do that for you.

  • Flexibility

All of the benefits of objects mentioned above help to create a more flexible system.


My question is, why do they use objects instead of arrays?

Probably two reasons:

  • WordPress is quite old
  • arrays are faster and take less memory in most cases
  • easier to serialize

Is there a performance reason for using an object?

No. But a lot of good other reasons, for example:

  • you may store logic in the objects (methods, closures, etc.)
  • you may force object structure using an interface
  • better autocompletion in IDE
  • you don't get notices for not undefined array keys
  • in the end, you may easily convert any object to array

OOP != AOP :)

(For example, in Ruby, everything is an object. PHP was procedural/scripting language previously.)