Asked • 07/29/19

PHP: when to use arrays, and when to use objects for mostly-data-storing code constructs?

PHP is a mixed paradigm language, allowing to use and return non-object data types, such as arrays. I pose a question to try to clarify some guidelines for selection of arrays vs objects when deciding upon what programming construct to use in a particular situation.This is really a question about ways to encode data using PHP language constructs and when one way is to be more likely picked over another for data passing purposes (i.e. Service-Oriented Architecture or web services).**Example**Suppose you have an item type consisting of {cost, name, part_number, item_count}. Your program calls for display of several such item types, to where you decide to use an array *as an outer container* to hold each of the item types. [You can also use PHP's `ArrayObject` for OO paradigm, but my question is not about *that* (outer) array]. My question is about how to encode the item type data, and about which paradigm to use. PHP allows you to use `PHP Native Arrays` or `PHP Objects`. I can encode such data, in two ways here, like so: //PHP's associative arrays: $ret = array( 0 => array( 'cost' => 10.00, 'name' => 'item1', 'part_number' => 'zyz-100', 'item_count' => 15 ), 1 => array( 'cost' => 34.00, 'name' => 'item2', 'part_number' => 'abc-230', 'item_count' => 42 ), );vs //here ItemType is encapsulated into an object $ret = array( 0 => new ItemType(10.00, 'item1', 'zyz-100', 15), 1 => new ItemType(34.00, 'item2', 'abc-230', 42), ); class ItemType { private $price; private $name; private $partNumber; private $itemCount; function __construct($price, $name, $partNumber, $itemCount) {..} }**What I am thinking**Array encoding is light-weight, and more JSON-ready, but can be easier to mess up. Misspell one of the associative array keys and you may have an error that is more difficult to catch. But it is also easier to change on a whim. Say I don't want to store `item_count` anymore, I can use any text-processing software to easily remove all `item_count` instances in the array and then update other functions that use it accordingly. It may be a more tedious process, but it is simple.Object oriented encoding calls upon IDE and PHP language facilities and makes it easier to catch any errors beforehand, but is harder to program and code up in the first place. I say harder, because you have to think a bit about your objects, think ahead, and OO coding takes a bit higher cognitive load than typing up array structures. That said, once it is coded up, some changes maybe easier to implement, in a sense, that removing `item_count`, for example, will require changing less lines of code. But changes themselves may still require a higher cognitive load in comparison with the array method, since higher-level OO facilities are involved.**Question**In some cases it is clear, like cases where I will need to perform manipulations on the data. But in some cases, where I need to just store a few lines of "Item Type" data, I don't have clear guidelines or considerations to lean on when trying to decide whether to use arrays or whether to construct objects. It seems I can just toss a coin and pick one. Is that the case here?

1 Expert Answer

By:

Julian L. answered • 11/06/19

Tutor
New to Wyzant

Industry Software Engineer

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.