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 Designing Interfaces in PHP Introducing Interfaces Implementing an Interface

Ray Knag
Ray Knag
8,110 Points

NULL Result, not sure what to adjust in my code of if this is a database.json issue

<?php

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

This code consistently returns the following:

"Warning: file_get_contents(/home/treehouse/workspace/srcdatabase.json): failed to open stream: No such file or directory in /home/treehouse/workspace/src/classes/jsonRepository.php on line 14

Notice: Trying to get property of non-object in /home/treehouse/workspace/src/classes/jsonRepository.php on line 15 NULL"

Not sure what I am doing wrong in this case, I did check out the other post about NULL results but I did not find that helpful for identifying the issue with my code.

1 Answer

Ray Knag
Ray Knag
8,110 Points

After giving this a day I just realized that my link in for jsonRepository in config.php was incorrect. I missed a "/" in front of database.json, whoops!