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

Alfredo Prince
Alfredo Prince
6,175 Points

My return should be an array. isn't that what I'm doing?

I'm certain i did something else wrong here. A little guidance would be helpful.

index.php
<?php

// add code below this comment
class Subdivision
{
public $houses = array();

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

?>
Corina Meyer
Corina Meyer
9,990 Points

There are several issues in your code that could have caused the challenge to fail.

  • You mixed up when to call a variable with $this->: it is used for class variables. the $this in that cases is the object (of that class' type) you look at. when you use a parameter in a method, you don't call it with this.
  • you don't need to repeat the $-sign after the arrow, instead of $house->$wall_color just do $house->wall_color.
  • your return-statement is misplaced. you want to run through the whole array of houses and then return something, don't you? :)
  • you should think about the return value. what does the challenge tell you to collect? perhabs you need to define a separate variable where that makes sense.
  • you can't combine if clauses like this. each side of the or operator must return true or false so you will have to repeat a whole statement.

i hope this helps.

Tagging Alena Holligan

I think there might be a problem with the challenge test on this one. It doesn't seem to be checking the values of the elements in the returned array.

2 Answers

Alfredo,

I think you and I covered this problem in another conversation. This is a confusing challenge. Here are a few details that the challenge doesn't give you that you need to know:

  1. The houses property is an array of House objects.
  2. The House class has three public member variables: lot, roof_color, and wall_color.

You are supposed to loop through the houses array and create a separate array that only contains the House objects that have a roof_color or wall_color equal to the color passed to the filterHouseColor method. The result looks something like this:

public function filterHouseColor($color) { 
    $matches = array();
    for ($i=0;$i<count($this->houses);$i++){ 
        if ($this->houses[$i]->roof_color == $color || $this->houses[$i]->wall_color == $color) { 
            $matches[] = array('lot' => $this->houses[$i]->lot); } } return $matches; 
        } 
    }
    return $matches;
}

Hi Eric,

The code could be a little easier with a foreach loop.

I think you're returning the wrong array structure here. You're creating a multi-dimensional array with the inner arrays being associative arrays.

It's supposed to be a single array of lot values.

$matches[] = $this->houses[$i]->lot;

That would add each lot value to the array.

Alena Holligan
STAFF
Alena Holligan
Treehouse Teacher

I updated the code challenge to be more specific and also return more hints to point you in the right direction. I'd appreciate it if people would check it out and give me any feedback on how else it could be improved. Alfredo Prince , Jason Anello , Eric Drake , Corina Meyer

Hi Alena,

Testing seems to be good now. I can't find any incorrect code that passes.

The instructions have changed for task 3. Is it ok that the existing answers for this question will no longer match up?

Alena Holligan
Alena Holligan
Treehouse Teacher

Thanks @jason! It's ok if they don't match, thanks for calling it out here. STEP 3 HAS BEEN CHANGED