PHP Designing Interfaces in PHP Introducing Interfaces Implementing an Interface

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