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

B P
B P
4,563 Points

Cant figure out why this wont run.. (PHP)

This is my code, which keeps telling me that "task 1 is no longer passing" and idk why..

<?php

// add code below this comment

class Subdivision { public $houses = []; private $matchingHouses = [];

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

}

?>

index.php
<?php

// add code below this comment


class Subdivision {
  public $houses = []; 
  private $matchingHouses = []; 

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

}


?>

1 Answer

Zachary Billingsley
Zachary Billingsley
6,187 Points

Hello!

Your idea for this challenge is good but it seems like the challenge wants you to add a variable just to the scope of the method.

So instead of using a private property for the class you can just use something like this...

<?php
$matchingHouses = [];
 foreach ($this->houses as $house) {
       if ($house->roof_color == $color || $house->wall_color == $color) {
         $matchingHouses[] = $house; 
       }
}
return $matchingHouses;

Hope that Helps!