0

Image I want to have an object $parent;

Where for example :

    $parent->firstname = "Firstname";
    $parent->lastname = "Lastname";
    $parent->children = ???

-> This would then have to be a collection of objects so that later I can do this :

    foreach ($parent->children as $child) { 
      $child->firstname
      $child->lastname
    }

Is this possible thing to do?

Disconv
  • 11
  • 1
  • 3
    `$parent->children` should be an _array_ of objects. Where do the children come from? That influences how you initialize the array. – Michael Berkowski Mar 07 '13 at 13:57
  • you really should be using getter and setter methods. not directly storing values to properties. – Nahser Bakht Mar 07 '13 at 14:00
  • 1
    See as well: [Array of objects within class in PHP](http://stackoverflow.com/questions/7812198/array-of-objects-within-class-in-php) – hakre Mar 07 '13 at 14:01

2 Answers2

0

Yes its possible, for example if you make children an array.

This is just example, this isn't best solution:

class person
{
    public $firstname = 'Jane';
    public $lastname  = 'Doe';
    public $children  = array();
}

$parent = new person();
$parent->firstname = "Firstname";
$parent->lastname  = "Lastname";

//1st child
$child = new person(); 
$child->firstname = 'aa';
$parent->children[]  = $child;

//2nd child
$child = new person(); 
$child->firstname = 'bb';
$parent->children[]  = $child;        

foreach ($parent->children as $child) {
    ...
}
hakre
  • 193,403
  • 52
  • 435
  • 836
Vahe Shadunts
  • 1,956
  • 14
  • 18
0

It depends a bit what you want. As your types are just property objects I think the solution by Vahe Shadunts is most lightweight and easy.

If you want to get more control in PHP you need to make use of getters and setters. This will allow you to make it work more specific.

As far as foreachDocs is concerned, all your children object needs to do is to implement the Iterator or IteratorAggregate interface and it then can be used inside foreach (see Object IterationDocs).

Here is an example:

$jane = ConcretePerson::build('Jane', 'Lovelock');

$janesChildren = $jane->getChildren();
$janesChildren->attachPerson(ConcretePerson::build('Clara'));
$janesChildren->attachPerson(ConcretePerson::build('Alexis'));
$janesChildren->attachPerson(ConcretePerson::build('Peter'));
$janesChildren->attachPerson(ConcretePerson::build('Shanti'));

printf(
    "%s %s has the following children (%d):\n",
    $jane->getFirstname(),
    $jane->getLastname(),
    count($jane->getChildren())
);

foreach($janesChildren as $oneOfJanesChildren)
{
    echo ' - ', $oneOfJanesChildren->getFirstname(), "\n";
}

The output:

Jane Lovelock has the following children (4):
 - Clara
 - Alexis
 - Peter
 - Shanti

These named interfaces and objects that work in the background here (I link the code at the end) have certain benefits compared with just arrays and properties if you need more functionality (e.g. over time).

Let's say Jane is married with Janet so they both share the same children so both share them:

$janet = ConcretePerson::build('Janet', 'Peach');
$janet->setChildren($janesChildren);

Now Janet gets a new child:

$janet->getChildren()->attachPerson(ConcretePerson::build('Feli'));

And so does automatically Jane because both share the same children object.

However PHP is not strong with these typed-collections therefore you have quite some boilerplate code to get this done.

code gist

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836