Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Object-Oriented PHP Basics Building a Collection Collection Objects

Gareth Redfern
Gareth Redfern
36,217 Points

Object-Orientated PHP Code Final Code Challenge - Collection Objects

Hi there I am really stuck with this question it seems to jump from creating a property and a method on the class which I get no problem to then access a house object with a let property?

index.php
<?php

// add code below this comment

class Subdivision

{

  public $houses = array();

  public function filterHouseColor($color)
  {

  }

}

?>
Gareth Redfern
Gareth Redfern
36,217 Points

This is the question:

Return an array containing the public lot parameter of each house object whose public roof_color OR wall_color match the passed color parameter.

3 Answers

Gareth, something along these lines:

class Subdivision {
  public $houses = [];

  public function filterHouseColor($color) {
    $lots = [];  //create array

    foreach ($this->houses as $house) {  //loop through array

      if ($house->roof_color === $color || $house->wall_color === $color) {
        $lots[] = $house->lot; 
      }
    }

    return $lots;  
  }
}
Gareth Redfern
Gareth Redfern
36,217 Points

Thank you jcorum, I was going down the road of trying to new up a house object and then add it to the houses array because I thought I would need a house object to work with. Is it me or is that question not very clear?

Your code worked great thank you.

Denis Rubanga
Denis Rubanga
2,907 Points

<?php

// add code below this comment class Subdivision { public $houses = [];

public function filterHouseColor($color) { $lots = []; //create array

foreach ($this->houses as $house) {  //loop through array

  if ($house->roof_color === $color || $house->wall_color === $color) {
    $lots[] = $house; 
  }
}

return $lots;  

} }

?>

Denis Rubanga
Denis Rubanga
2,907 Points

$lots[] = $house->lot; needs to be changed to $lots[] = $house;