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

Hey guys is anyone able to help me with part 3 of the collection objects challenge?

My attempt at an answer is attached, but I think I am over thinking it.

index.php
<?php


// add code below this comment
class Subdivision {

  public $houses = array();

  public function filterHouseColor($color) {

    $filteredColor = array();
    foreach ($houses as $key => $value) {
      $item = $value['i'];
      if  ($item === $color) {
        $filteredColor[] = $item;
        }
    }

    return $filteredColor;

    }

  }


class house {

  public $roof_color;
  public $wall_color;

 }

$local_house1 = new house->roof_color("red");
$local_house1 = new house->wall_color("white");
$local_house2 = new house->roof_color("grey");
$local_house2 = new house->wall_color("blue");
$local_house3 = new house->roof_color("orange");
$local_house3 = new house->wall_color("brown");

$house_array = array_push($SubDiv_Ob = Subdivision->$houses, $local_house1, $local_house2, $local_house3);

$SubDiv_Obj = new Subdivision;
$Subdiv_Obj->filteredColor($red);




?>

1 Answer

OK so I have updated it a bit but it still isn't happy. I am sure there are other problems with it but for some reason it is returning an error because it won't accept the declaration of an object as 'new House' because it has already been declared as the class name. Which needless to say makes no sense. Might it be better to make the house class static?

<?php

// add code below this comment class Subdivision {

public $houses = array();

public function filterHouseColor($color) {

$filteredColor = array();
foreach ($houses as $key => $value) {
  $item = $value['i'];
  if  ($item === $color) {
    $filteredColor[] = $item;
    }
}

return $filteredColor;

}

}

class House {

public $roof_color; public $wall_color;

}

$local_house1 = new House; $local_house1->roof_color("red"); $local_house1->wall_color("white"); $local_house2 = new House; $local_house2->roof_color("grey"); $local_house2->wall_color("blue"); $local_house3 = new House; $local_house3->roof_color("orange"); $local_house3->wall_color("brown");

$SubDiv_Obj = new Subdivision; $house_array = array_push($SubDiv_Ob->houses, $local_house1, $local_house2, $local_house3); $Subdiv_Obj->filteredColor($red);

?>