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

Leave a Comment