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

Ray Knag
Ray Knag
7,807 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
7,807 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!