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?

3
  • 3
    $parent->children should be an array of objects. Where do the children come from? That influences how you initialize the array. Commented Mar 7, 2013 at 13:57
  • you really should be using getter and setter methods. not directly storing values to properties. Commented Mar 7, 2013 at 14:00
  • 1
    See as well: Array of objects within class in PHP Commented Mar 7, 2013 at 14:01

2 Answers 2

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) {
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.