I have some input that I send to a computational library, which processes the input and produces some output.

Question: What considerations do I take into account to help me decide whether the output data type should be an array, or an object? Similarly the same can apply to an Input object that's fed into the library.

Example Array:

    $t = array();
    $t['number_of_shells'] = 3;
    $t['shell_length'] = 2.51;
    $t['pipe_model'] = 2.38;

Example Class

    $t = OutputObject();

Where it will be used

In my immediate case, this structure will be used to accumulate some data from several various sources (computational library, database, pass-through-input) and then passed to the View layer, where it will be plugged into placehodlers of the view template.

More generally

From where I see it, arrays are easier to create, easier to maintain, as in my case all I need is basically values. Objects are more robust in that they hardcode the available values into properties and there is less of a chance of making a typo. But they also add some overhead.

I will use the data structure as a return type of the computational library, and later use it for data to feed to my View layer, or to my Unit Test suite.

  • 1
    The advantages and disadvantages are basically the same as those between strongly-typed and untyped objects. Evaluate those advantages and disadvantages, and make a decision. Commented Mar 3, 2016 at 16:29

2 Answers 2


From my aged experience with PHP, a good design is such that, even though PHP allows you to do that, you do not have to annotate anything with the @var annotation to get code-completition on a variable or you do not need to open a definition of a method to find out which indexes are accessible.

Please note, the following suggestions are standards we hold where I work and are not generally applied.

Return an array only if you return scalar data type

If a service (a class) is returning an array of items which is number-indexed by default (0, 1, ...n) or indexed by ids passed to it and it returns only one data type, it is not usually necessary to return an object, because you very well know what is going to be in the array.

Scalar data types such as strings, doubles (floats), ints, and the array should not be nested.

If you want to return more complex structures such as associative arrays with custom indexes, objects are the way to go.

Many modern IDE's will allow you to iterate over a collection of custom classes and should a method (which populates the variable you want to iterate over) be annotated with the @return CustomClass[] will suggest you methods for the single subject within one iteration cycle, member methods, which the CustomClass offers.

If you need to go as far as returning multi-dimensional arrays of objects, you can (and if you do not want to prefix anything with the @var annotation you pretty much have to) implement the Iterator interface. By doing so even when nesting multiple foreaches, you should still have code-completition available.


In a loosely-typed language such as PHP, associative arrays and classes behave much the same as far as storing data is concerned. However, there is one major difference.

Arrays require strings or integers to reference the "fields," while classes have actual data members.

If you mistype an array key, it will silently retrieve nothing, or store data in a location other code will not reference. If you mistype a data member or function name, you get an error.

Using a class for this is much safer.

  • The decision will probably come down to whether or not the OP wants to provide a custom class for every possible output in exchange for type safety. I personally prefer strong typing, but there are days (like the one where I had to pick out a couple of fields from a very complex JSON schema) where loose typing rules the day. Commented Mar 3, 2016 at 16:30
  • @RobertHarvey which is why I did not say a class is "better," I said "safer." I agree, this is a case of the axiom "different tools for different jobs."
    – user22815
    Commented Mar 3, 2016 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.