3

Would it make more sense in the following situation to create a new object type instead of using an array:

I have a simple structure with two values: Name & Age

$ages = array(
     array(
       'name' => 'bill',
       'age'  => 22
    ),
    array(
       'name' => 'bob',
       'age'  => 50
    ),
    // etc
);

The problem is that this structure is generated in one class and passed through a controller and then used in another class.

Therefore it feels like those two classes are tied together as one must know the array keys of this structure that is generated in another.

Is there any design pattern that solves this?

4 Answers 4

1

I don't think you need (of even if there is) a design pattern for generating an object/data structure in one class, and consuming it in another. It is the basic premise of working with classes. Also, as alfasin mentioned, working with objects are neater than arrays. Also in the future, you could have better interactions with other objects, if such a need arises.

1

I would go all the way and define a Person model class. Something like this

Class Person {

  protected _age;

  protected _name;


  public function __construct($name = null, $age = null) {
    if ($name) setName($name);
    if ($age) setAge($age);
  }

  public function getName() {
    return $this->_name;
  }

  public function setName($name) {
    return $this->_name = (string) $name;
  }

  public function getAge() {
    return $this->_age;
  }

  public function setAge($age) {
    return $this->_age = (int) $age;
  }
}

You can then use this class to create your datastructure as follows:

$persons = array(new Person('bill', 22),new Person('bob', 50));

This array can then be passed by your controller and used like this in the view:

foreach($persons as $person) {
  echo $person->getName();
  echo $person->getAge();
}

This design pattern is called MVC (Model View Controller) and very popular and well documented, although interpretations my differ.

This might look like overkill for your simple structure, but it will save you a lot of time when having to extend your code in the future.

(the code is not tested, but i think it should work just fine)

1
  • This is not MVC as you might have a model (data) but there is no view (arguable cause you can consider the usage as a 'view') and certainly no controller.
    – Nir Alfasi
    Aug 24, 2012 at 18:25
0

Since it's a simple structure you can work with it, but in general it's recommended to work with objects. If you'll want to add fields in the future, add levels (nested arrays) - maintenance will be easier as you're program will be more modular and less coupled:

// I - easier to use
$bill_age = $ages->get_age('bill'); 
// function get_age() is implemented in the class which 
// makes you code easier to maintain and easier to understand

// II - this implementation is dependent on the structure of $ages 
// if you'll change $ages - you'll have to change all the uses:
$bill_arr = $ages[0];
$bill_age = $bill_arr['age'];

Further, if you'll have calls like II on different places in the code, changing $ages structure will result in changing all these places, while if you implement I - you have only one place in the code to change (the implementation of get_age($name) inside the class).

0

I think you can have one class which will contain the Keys for this structure and then both class will share this class to get key instances. In that way you won't have to keep track of keys in both the classes. Moreover, anytime you can add more keys without changes much here n there. Less Coupling and More Flexibility.

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.