PHP - spl_autoload and namespaces - doesn't work with capital letters

php namespace
php autoload namespace
spl_autoload_register not working
spl_autoload_register vs __autoload
php autoload functions
how to use spl_autoload_register php
spl_autoload_register not loading class
php spl_autoload_register multiple directories

I've set up a really basic autoloader in my index.php to grab a namespaced class within hello.php. My development environment is Ubuntu 12.04.

Why am I trying to do this? I'm trying to stick to the PSR-1 and PSR-2 coding standard, that includes:

Class names MUST be declared in StudlyCaps

Namespaces are as /Vendor/Class (note: capitals)

The following is my structure and code that works before making the changes to capitals.

Folder Structure

- web
  -- index.php
  -- core
    --- hello.php

Autoloader

Within index.php, I have my autoloader:

set_include_path(__DIR__);
spl_autoload_extensions('.php,.class.php');
spl_autoload_register();

Class File

Within the core folder, I have hello.php

namespace core;

class hello {
    public function __construct() {
        echo 'Constructed!';
    }
}

Code that works

If I run $obj = new \core\hello(); in my index.php, I get back "Constructed!". Great!


That which doesn't work

Renaming my core folder to 'Core' - note the uppercase C, and also the namespace in hello.php to namespace Core;.

Now let's try again with $obj = new \Core\hello();

Fatal error: Class 'Core\hello' not found in ...

So please tell me, why am I not able to use capital letters to keep inline with the PSR standards? What am I doing wrong?

When you run your PHP code on a Linux platform, it's important to remember that Linux is case sensitive with filenames.

This affects autoloaders because they typically use the namespace and the class name when building the filename to load.

If the folder is named core, then the namespace must be core, with the same capitalisation. If you change it to Core in the namespace, then you must do the same to the folder name. (and as a result, all other core classes must be changed to Core at the same time).

On Windows, this doesn't happen because the Windows filesystem isn't case sensitive. This can cause confusion when code is tested and works on a local Windows-based dev system, and then breaks when it is copied to a Linux-based server.

[EDIT]

Okay, so I missed that you had changed the dirname as well. But nevertheless, I still think this is an issue of the filename/dirname case.

I note that you're calling spl_autoload_register() without any params. This means that the default spl_autoload() function will be used as the autoloader.

If you read the documentation for spl_autoload(), you'll note that it uses the lowercased version of the class and namespace.

In other words, using the default autoloader, your classes can be mixed case, but the folder structure and filenames must be all lower case.

So in fact, for you, you need to keep your filenames lower case.

I've personally experienced it the other way round, as per my original answer, where I had a fully lower case filename, and my mixed case class name was breaking when I moved from Windows dev box to Linux server. The reason my experience is different from yours is because I'm using a custom-written autoload function, which doesn't do an auto-lowercase conversion, so the case of my filenames has to match that of my classnames.

PHP namespacing and spl_autoload_register, So the problem was that the $class being returned to spl_autoload_register was the namespace\class name, with the backslash intact. If you want to make the best use out of autoload with an APC cache don't use spl_autoload. It uses relative paths and thus will perform a stat even with apc.stat=0 (either that, or it doesn't work at all).

I think you have shown us some good ambiguity.Correct me if I am wrong.

According to the specification you have to use the lowercased name of the class (and namespace) being instantiated.(http://www.php.net/manual/en/function.spl-autoload.php)

But PSR tells us to use the capital letters. If you want to stick with PSR then we have to overwrite the default spl_autoload to our own.

Autoloading Classes with Namespaces in PHP, Learn everything about php autoloading. You'll get the above error because any programming language doesn't find class files and include� Extensions doesn't have to start with a dot, spl_autload() will simply append whatever you supply to the basename. The following example will try to load "test.php" first, and "test/index.php" as well:

For anyone else having issues with this, why not make use of ucfirst() or strtolower() ?

So the code below would try all lowercase and also try first letter uppercase files

e.g: somename.class.php or Somename.class.php

The is_readable() checks first as to not display the php error for file not found.

spl_autoload_register(function($name) {

    if (is_readable(strtolower($name).'.class.php')) {
        require_once(strtolower($name).'.class.php');
    } 
    elseif (is_readable(ucfirst($name).'.class.php')) {
        require_once(ucfirst($name).'.class.php');
    }
});

Advanced PHP: Autoloading Classes and Namespaces, Hi everyone, Edit: If you are a beginner, check out the complete autoloading tutorial. Today we are going to discuss some advanced stuff in� It is a bad practice to add spl_autoload_register in all the php scripts. So, create a file called autoload.php and save it in the root directory (or in a directory you have include files). Then you just have to include autoload.php in a php script to register the autoload function.

Autoloading classes, interfaces or traits in PHP 7, if the class ClassName doesn't exist (because it hasn't been included), PHP Autoloading is the process of automatically loading PHP classes without <?php namespace classes; class home { public function get(){ return� Questions: According to the top comment on the PHP page spl_autoload_register( ) : Good news for PHP 5.3 users with namespaced classes: When you create a subfolder structure matching the namespaces of the >containing classes, you will never even have to define an autoloader. <?php spl_autoload_extensions(".php"); // comma-separated list spl_autoload_register(); ?> However, when I have

Autoloading PHP classes that use namespaces, As a result, we need to be able to handle these backslashes inside our autoloading function. Our namespace class. Let us take a look at an example of a PHP� Since PHP 5.3, you can use spl_autoload_register() with namespaces, which means that you can organize your project and autoload your php classes without any require or include. by BrainBell updated Feb 08, 2019

Autoloading Classes, Autoload; SPL Autoload; Autoload File; Composer <?php namespace ArrayUtil ; class ArrayUtil { public static function min($array): float { return \min($array); } }. Basically your namespace represents you directory structure and classes can be loaded based on a convention. If the PSR-0 method doesn't meet all your needs (or doesn't play nice with existing code) you can still add more functions with spl_autoload_register and PHP will go through them one by one in an attempt to load classes. Example usage:

Comments
  • dirname(__FILE__) is unnecessary; you can just use __DIR__.
  • Thanks! I'll amend...
  • This doesn't help unfortunately - as already stated the change has definitely been made from core to Core in the object declaration, the folder name and the namespace in hello.php.
  • hm, Okay, I missed the bit about you chaning the directory as well. sorry. but hang on -- I think I've spotted the problem... I'll just edit the answer....
  • Ahh, I see. I saw the following in one of the comments on there: spl_autoload_register(function($class) { return spl_autoload(str_replace('\\', '/', $class)); }); - any advances on that as it doesn't seem to work - to allow uppercases?
  • @Jimbo - no, if you want to keep your filename case the same as the classnames, I think you'll have to write your own autload function rather than using spl_autoload() at all. It doesn't have to be complex; mine is only a dozen lines or so. In fact, you could take the example autoloader code from PSR-0.
  • I thought as much - thank you for pointing this out though, it helped a lot and I'm sure it'll be useful to others in the future :)
  • Actually, the best tool for the job now is Composer. If you start using it, it literally sorts out all your namespaces for you and you only need to include vendor/autoload.php at the top of your file to have all your namespaces automatically resolved.
  • I got introduced with composer while working on Zend2 framework installation.It's a good tool indeed plus I also would like to explore under the hood.