PHP - spl_autoload and namespaces - not working with capital letters

I installed a really basic autoloader in my index.php to grab a class with names in hello.php . My development environment is Ubuntu 12.04.

Why am I trying to do this? I am trying to adhere to the PSR-1 and PSR-2 encoding standard , which includes:

Class names MUST be declared in StudlyCaps

Namespaces: / Vendor / Class (note: capitals)

Below is my structure and code that works before , making changes to capitals.

Folder structure

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

Autoload

In index.php, I have an autoloader:

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

Class file

Inside the main 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 will return "Constructed!". Excellent!


What does not work

Renaming my main folder to "Core" - pay attention to the upper case C, as well as the namespace in hello.php - namespace Core; .

Now try again with $obj = new \core\hello();

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

So, please tell me why I can not use capital letters to support PSR standards? What am I doing wrong?

+4
source share
3 answers

When you run your PHP code on the Linux platform, it is important to remember that Linux is case sensitive with file names.

This affects autoloaders because they typically use the namespace and class name when creating the download file.

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

On Windows, this does not happen because the Windows file system is not case sensitive. This can cause confusion when checking code and working with a local Windows-based system, and then breaks when copying to a Linux-based server.

[EDIT]

Ok, so I skipped that you changed the name dirname too. But still, I still think this is a problem with the file name / dirname.

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

If you read the documentation for spl_autoload() , you will notice that it uses a version of the class and namespace with a lower base.

In other words, using the default autoloader, your classes may be mixed, but the folder structure and file names should be lowercase.

So, for you, you need to keep your names lowercase.

I personally experienced this in the reverse order, according to my original answer, where I had the full file name with lowercase, and my mixed case class name broke when I switched from the Windows dev window to the Linux server. The reason my experience is different from yours is because I use a specially written autoload function that does not perform conversion with automatic lower case, so the case with my names should match my class name.

+7
source

I think you showed us some good ambiguity. Bring me back if I'm wrong.

According to the specification, you should use the lowercase name of the class (and namespace) ( http://www.php.net/manual/en/function.spl-autoload.php )

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

+2
source

For everyone who has problems with this, why not use the ucfirst () or strtolower () functions?

So, the code below will try all lowercase letters, as well as try upper case letters of the first letter

e.g. somename.class.php or somename.class.php

The is_readable () function first checks to not display a php error for a file that is 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'); } }); 
+1
source

All Articles