Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
PHP class name constant (::class) is case insensitive and that might break your PSR-11 container access

PHP´s magic ::class-constant will not canonical the casing of your imports.

This can lead to hard to debug errors when you a using a case sensitive service PSR-11-locator like Zend\ServiceManager.

namespace FirstNamespace;

class TestClass {}
namespace YetAnotherNamespace;

use FirstNamespace\Testclass; // wrong case in last segment

$instance = new TestClass(); // perfectly valid since php itself is not case sensitive for classnames

// TestClass::class expands to FirstNamespace\Testclass which is not registered in the $conainer

Discussion on Twitter:

Please note that this is not a bug in PHP but expected behaviour: "The class name resolution using ::class is a compile time transformation. That means at the time the class name string is created no autoloading has happened yet. As a consequence, class names are expanded even if the class does not exist. No error is issued in that case.". See:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.