<?php
var_dump(php_uname('s'));
$base_dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR;
file_put_contents($base_dir . 'Baz.php', "<?php namespace Foo\Bar; class BAZ {}");
file_put_contents($base_dir . 'Buz.php', "<?php namespace Foo\Bar; class Buz {}");
spl_autoload_register(function ($class) {
echo "Trying to autoload $class\n";
// project-specific namespace prefix
$prefix = 'Foo\\Bar\\';
// base directory for the namespace prefix
global $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;
} else {
echo "Could not require $file\n";
}
});
// This works as expected because the class name matches the intended
// case in the file and as it is represented on disk.
$c1 = new \Foo\Bar\Baz();
// This works because PHP is PHP. This is not even going to hit the
// PSR-4 autoloader. The *usage* of this classname by the user is not
// PSR-4 compliant but that does not make the *autoloader* noncompliant.
$c2 = new \Foo\Bar\BAZ();
var_dump($c1 == $c2);
// This is going to be rejected by the PSR-4 autoloader implementation.
// That is fine *and expected* because the *usage* of the classname by
// the user is incorrect.
//
// On some platforms this may actually work but that is a byproduct of
// certain platforms not caring about case. But again, the *usage* of
// this classname *in this context* is not PSR-4 compliant.
//
// PSR-4 says that case is important and that case MUST be taken into
// account when creating the file path representing the class name. It
// also dictates that case MUST be taken into account when you create
// files on disk for the FQCN in question.
//
// If the FQCN is \Foo\Bar\Baz in src/Baz.php, then specifying the class
// with any other case than \Foo\Bar\Baz by the user is not compliant
// with PSR-4.
//
// In some cases the planets will align and allow this to work anyway.
// If you rely on and promote this, you are actively noncompliant with
// PSR-4.
$c3 = new \Foo\Bar\BUZ();
$c4 = new \Foo\Bar\Buz();
var_dump($c3 == $c4);
Output for git.master, git.master_jit, rfc.property-hooks
string(5) "Linux"
Trying to autoload Foo\Bar\Baz
bool(true)
Trying to autoload Foo\Bar\BUZ
Could not require /tmp/BUZ.php
Fatal error: Uncaught Error: Class "Foo\Bar\BUZ" not found in /in/kssKp:74
Stack trace:
#0 {main}
thrown in /in/kssKp on line 74
Process exited with code 255.
This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.