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

I am not completely clear on what I am doing here. Can anyone clarify the instructions?

I've been slightly confused in the least few videos and examples.

index.php
<?php

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

  public static function filterHouseColor($color){
  Subdivision::filterHouseColor($color);
  }

  }
}
?>

Hey Keith - Give this a shot:

<?php

class Subdivision
{
  public $houses;

  public function filterHouseColor($color)
  {
    $filteredHouses = [];    

    foreach ($this->houses as $house)
    {
      if ($house->roof_color == $color || $house->wall_color == $color) {
        $filteredHouses[] = $house;
      }
    }

    return $filteredHouses;
  }
}

?>

Cheers,

Ben

Hi Ben, your answer is correct. I am understanding up to the filteredHouses array. I feel as if I am getting lost in the instructions.

Also, is your entry marked as an answer? I can't seem to mark it as the correct answer.

1 Answer

Hey Keith - Here is a little more context. I added a House class so you can kind of see what this would look like. Hopefully that helps.

<?php

class House
{
    public $roof_color;

    public $wall_color;

    /**
     * House constructor.
     * @param $roof_color
     * @param $wall_color
     */
    public function __construct($roof_color, $wall_color)
    {
        $this->roof_color = $roof_color;
        $this->wall_color = $wall_color;
    }
}

class Subdivision
{
    private $houses;

    /**
     * Subdivision constructor.
     * @param $houses
     */
    public function __construct($houses)
    {
        $this->houses = $houses;
    }

    public function filterHouseColor($color)
    {
        $filteredHouses = [];

        foreach ($this->houses as $house) {
            if ($house->roof_color == $color || $house->wall_color == $color) {
                $filteredHouses[] = $house;
            }
        }

        return $filteredHouses;
    }
}

$houseA = new House('green', 'grey');
$houseB = new House('brown', 'yellow');

$subdivision = new Subdivision([$houseA, $houseB]);

$filteredHouses = $subdivision->filterHouseColor('green');

?>

Cheers,

Ben

Thank you so much Ben. That helps out a ton.