Skip to main content
Question Protected by gnat
edited tags - added perl because it has associatives arrays similar to PHP
Source Link
Dennis
  • 8.3k
  • 5
  • 37
  • 70

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

Example

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

Tweeted twitter.com/#!/StackProgrammer/status/642123404917776384
added 76 characters in body
Source Link
Dennis
  • 8.3k
  • 5
  • 37
  • 70

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?

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. It seems I can just toss a coin and pick one. Is that the case here?

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?

Source Link
Dennis
  • 8.3k
  • 5
  • 37
  • 70

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.

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. It seems I can just toss a coin and pick one. Is that the case here?