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 trialFrederick Rogers
25,142 PointsImplementing 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 );
}
}
}
}