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

alexander Towell
PLUS
alexander Towell
Courses Plus Student 6,767 Points

Could i have a hint for this question? Ive tried everything

After a day or two of trying, I can't figure out whether or not I'm supposed to create instances of the house object for the array or not ?

index.php
<?php

// add code below this comment
class Subdivision {
    public $houses = array(); 
    public function filterHouseColor($color){
      $array = array();
      if($color = "roof_color" || $color = "wall_color")
      {
          $array[] = $this->$houses->$house->$lot;
      }

      return $array;
    }
}
?>

5 Answers

Hi Alexander,

It's been a while since I've written any PHP so this might be a little different than what you've been learning... so keep that in mind. Here's the solution:

<?php
class Subdivision {
  public $houses = []; // Same thing as array()
  public function filterHouseColor($color) {
    $lots = []; // just a temporary array 

    // Looks like this is where you got lost
    // In order to access the $houses array, you need to do $this->houses because $houses is a property on Subdivision
    // This foreach loop loops through the $houses array
    // So each cycle, $house contains a $house object found in $houses
    foreach ($this->houses as $house) {
      // Remember: you can only access $roof_color and $wall_color from the $house object
      if ($house->roof_color === $color || $house->wall_color === $color) {
        $lots[] = $house->lot; // Here, we append the value of each $house's $lot property to that temporary array up there named $lots
      }
    }

    return $lots;  // Finally, we return $lots
  }
}
?>

Let me know if you have any questions ;)

Vinícius Caldas
Vinícius Caldas
16,372 Points

I've tried this code and it dosen't work! I dont know how to pass this chalenge... :(

Hi Vinícius,

You might have already figured this out by now but the Challenge is actually looking for you to return a filtered array of $house objects — as opposed to what I did, which was return the $lot property of each $house in the filtered array.

Nada Mabrouk
Nada Mabrouk
3,609 Points

Why can't i pass the challenege?

class Subdivision{
public $houses = array();
public function filterHouseColor($color){
  $myhouses = array();
  foreach($this->houses as $houseObject){
    if(($houseObject->roof_color === $color) || ($houseObject->wall_color=== $color)){
      $houses[] = $houseObject;
    }
  }
  return $myhouses;
}
}

Hi Nada,

You're appending each $houseObject to $houses rather than $myhouses. Fix that and it should pass :)

Mikis Woodwinter was much more thorough than I will be. But the short answer to the posted question is no, we don't need to create instances of the house object, with the new keyword. The challenge didn't ask to create new instances. I believe it referred to an array of objects, which means the instances are already there in the air somewhere to be pulled. Very abstract and confusing, at least for me still.

Hey Marie,

The instances of $house do not exist yet. But, that’s okay; the filtering function will still work okay. If you like, you could add a conditional that checks whether there are any $house instances in $houses (and if so, exit the function early)... but it’s really not needed in this case. If it helps, try imagining there is another function on the Subdivision class called “addHouse($house).”

Oh, ok, thanks for clearing that up. So does that mean that instances are not necessary, or are the $house objects in the $houses array in your example in the foreach loop- are those the new instances? And by the way, what is a temporary array? Why would the $lots[] array be temporary?