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

Implementing an interface - $repo->find('posts', 1) returns NULL

Hi I am having some trouble with this portions of the video lesson on "Implementing an interface". at the 07:32 mark we are asked to var_dump() the return value of the find() method on the jsonRepository (initiliased as $repo) object. How ever although the previous var_dump() of the method all() works just fine. The find() method simply returns NULL. Not sure why this is.

Here is my php code for the jsonrepository class:

class jsonRepository implements RepositoryInterface
{

  protected $file;

  public function __construct( $file ) 
  {
       $this->file = $file;
  }

  public function all( $entity ) 
  {
    $data = json_decode( file_get_contents( $this->file ) );

    return $data->$entity;    
  }

  public function find( $entity, $value, $field = 'id' ) 
  {
      foreach( $this->all( $entity) as $key => $data ) {
        if ( $data->$field === $value ) {
          return array( $data );
        } 
      }
  }

}