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

Alex Warner
PLUS
Alex Warner
Courses Plus Student 4,987 Points

I don't understand this Code Challenge

The instructions in this code challenge are too vague. I successfully completed steps one and two. I then, to the best of my understanding of the instructions in Step 3, wrote the code. I'm getting an error but if I had better instructions I could complete the challenge. I'm experienced with OOP PHP but it seems like the author of this challenge is making assumptions that aren't conveyed to the end user.

index.php
<?php

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

  public function filterHouseColor($color)
  {
    $lot = array();
    foreach ($houses as $house) {
      if ($house->roof_color ==  $color || $house->wall_color) {
      $lot[] = $house->lot;
    }
    return $lot;
  }
}

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Alex,

I completely agree and feel your pain. With that said and how 'stuck' I am with this other challenge I am just going to give you the code I figured out for yours (I actually save all my answers), but I don't really remember why or how I came to this answer. I hope it makes sense for you and helps you out. :)

<?php

class Subdivision
{
  public $houses = array();

  public function filterHouseColor($color)
  {
    $arr = [];
    foreach($this->houses as $ahouse)
    {
      if($ahouse->roof_color === $color || $ahouse->wall_color === $color) {
        $arr[] = $ahouse-lot;
      }
    }
    return $arr;
  }
}

?>

Keep Coding! :dizzy:

Hi Jason,

I had noticed you forgot the greater than symbol for the object operator inside your if block.

$arr[] = $ahouse-lot; // should be $arr[] = $ahouse->lot;

The code still passed though so I think there is a problem with this challenge.

Alex Warner
PLUS
Alex Warner
Courses Plus Student 4,987 Points

Thanks for the reply! Ha! I knew I had it right, was going nuts trying different options.