PHP Designing Interfaces in PHP Introducing Interfaces Implementing an Interface

Mister Moody
Mister Moody
38,288 Points

Implementing an Interface

Hey ya'll,

I'm stuck up a creek without a paddle. I have reviewed the code and all looks well, but I get this:

PHP Fatal error: Interface 'RepositoryInterface' not found in /home/treehouse/workspace/src/classes/jsonRepository.php on line 3

If you have some time to spare, I kindly ask that you preview my snapshot

Woke up today with this on my mind and with a few moments to share learned that in the config.php file, when I remove the final line that instantiates the $repo class, no error occurs, so at the moment I am led to believe that

$repo = new jsonRepository(__DIR__ . '/database.json'); 

is the issue~

Ben Payne
Ben Payne
1,464 Points

Hey MM - It looks like in your autoloader you are breaking the loop once you find the first class? Maybe try looping through everything and try again?

<?php
// AUTOLOAD "REGISTRATION" FUNCTION
/* Autoload Function = Never Load a Class Again! */
function autoloader($class_name)
{
    /* Loop through '/src' DIR in Search of File */
    foreach (glob(__DIR__ . '/*', GLOB_ONLYDIR) as $dir) {
        if (file_exists("$dir/" . $class_name . '.php')) {
            /*Require File 'once' IF Exists*/
            require_once "$dir/" . $class_name . '.php';
            /* Stop Loop 'after' File has been found */

            /* Try commenting this out */
            break;
        }
    }
}

// REGISTER 'autoload' FUNCTION
spl_autoload_register('autoloader');

// INSTANTIATE CLASS
/* PASS jsonRepository.php File through Current Directory */
$repo = new jsonRepository(__DIR__ . '/database.json');
?>

2 Answers

Mister Moody
Mister Moody
38,288 Points

Tried your suggestion @benpayne0925, to no avail. Nonetheless, I appreciate your support!

Ben Payne
Ben Payne
1,464 Points

So...don't get too mad, but the RepositoryInterface in your snapshot is misspelled. See below:

<?php

interface RespositoryInterface { // RepositoryInterface is mispelled.
  /* Retrieve ALL Items */
  public function all($entity);
  /* Retrieve a Single-Item */
  public function find($entity, $id, $field = 'id');

}

?>

I removed the extra s and it seems to work now.

Cheers,

Ben

Mister Moody
Mister Moody
38,288 Points

Thanks Ben. Not beating myself up; grateful you were here to assist! I owe you a beer~ hit me up if you are ever in (Louisville) Kentucky!