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?
(object)['foo'=>'bar']
. The resulting value has classStdClass
, will be JSON-encoded in its entirety byjson_encode()
and properties can be renamed as easily as array indices (which isn't always so easy, when it's accessed indirectly via a variable). However, there are different operations on such values; for example, you don't have object unions as you have array unions, and you can't directly use thearray_*()
functions.array
, 2:User-defined Class
, 3:stdClass
. The performance in terms of speed is pretty much the same when comparingarray
and aUser-defined class
(such as yourItemType
), but definedclass
es tend to use less memory than usingarray
s.stdClass
on the other hand is the slowest of the three options and also uses the most memory.