Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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);

?>