Member Avatar for diafol

Hi all. Been playing with some autoloading but couldn't get it to work. I'm trying to implement the PSR-4 (project scope) approach, but documentation is sketchy and I'm a bit lost as to the issue. I've read the PHP-Fig docs: http://www.php-fig.org/psr/psr-4/ and an trying to implement a script from the examples file: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md

My Directory Structure:

-src
 - Baz
  - Spotify.php

Here's the script (included in my index.php page):

/**
 * An example of a project-specific implementation.
 * 
 * After registering this autoload function with SPL, the following line
 * would cause the function to attempt to load the \Foo\Bar\Baz\Qux class
 * from /path/to/project/src/Baz/Qux.php:
 * 
 *      new \Foo\Bar\Baz\Qux;
 *      
 * @param string $class The fully-qualified class name.
 * @return void
 */
spl_autoload_register(function ($class) {

    // project-specific namespace prefix
    $prefix = 'Foo\\Bar\\';

    // base directory for the namespace prefix
    $base_dir = __DIR__ . '/src/';

    // does the class use the namespace prefix?
    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) !== 0) {
        // no, move to the next registered autoloader
        return;
    }

    // get the relative class name
    $relative_class = substr($class, $len);

    // replace the namespace prefix with the base directory, replace namespace
    // separators with directory separators in the relative class name, append
    // with .php
    $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
    // if the file exists, require it
    if (file_exists($file)) {
        require $file;
    }
});

new \Foo\Bar\Baz\Spotify;

This should load the class in src/Baz/Spotify.php called Spotify. The Spotify.php file content:

class Spotify
{
    public function __construct()
    {
        echo "yes"; 
    }
}

This is the error:

Fatal error: Class 'Foo\Bar\Baz\Spotify' not found in xxxxxxxxxxxxxxx\autoloader.php on line 44

This is the line:

new \Foo\Bar\Baz\Spotify;

Strangely the file is loaded and I did a class_exists() inside the file_exists() conditional and everything looked fine:

if (file_exists($file)) {
    require $file;
    if(class_exists($relative_class)){
        echo "I exist";
    }
}

I got the message and then the error message mentioned. Am I missing something? I bet it's something really stupid and I'll look like a prize pumpkin, heh heh.

JorgeM commented: If only everyone would post their questions in this manner. Sorry, can't help with an answer though... +12

Hi! Define the namespace in Spotify.php:

<?php namespace Foo\Bar\Baz;

class Spotify
{
    public function __construct()
    {
        echo "yes"; 
    }
}

Then it should work fine.

commented: Big hand. Thank you +15

I totally agree with cereal. That should work. The script from github was written by PHil Sturgeon. The same Phil who wrote the Pyro CMS build on top of CI and CI core contributor.

Member Avatar for diafol

Ha ha. What a numpty. Thanks cereal - it did the trick. Didn't know that veedeoo. Always good to learn. :)

Hope this post helps others also having a senior moment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.