Composer won't autoload my utility class (empty namespace field)

I’ve got the following utility class:

Now that I want to use that in another project of mine I’ve added it to my composer.json and composer install installed it fine. However when I try to use it

namespace Problem;

require __DIR__ . '/../vendor/autoload.php';

Util\LockFile::test(array('path' => __DIR__ . '/LOCK'));

I’m getting “Fatal error: Class ‘Problem\Util\LockFile’ not found in…”. If I take a look at vendor/composer/autoload_namespaces.php I’ve got the following

return array(
    'Symfony\\Component\\Yaml\\' => array($vendorDir . '/symfony/yaml'),
    'LazyMap\\' => array($vendorDir . '/ocramius/lazy-map/src'),
    'Instantiator\\' => array($vendorDir . '/ocramius/instantiator/src'),
    '' => array($vendorDir . '/marlun/php-util-lockfile'),

What am I doing wrong?

Notice that it’s looking for Util\LockFile relative to the current namespace that you’re in. Try:


Notice the leading backslash. This tells PHP to start looking from the root namespace rather than from within your Problem namespace.

Still getting “Fatal error: Class ‘Util\LockFile’ not found in…”.

The namespace on the utility class is Problem\Util.

Update: I also found that my util filename was lockfile.php instead of LockFile.php which is now changed but that didn’t fix my problem, still getting the same error.

Ahh, yes. Ignore what I said then. :blush:

The next likely issue (though I haven’t run any code to test and be sure) is that there isn’t a good mapping between your utility class’s filesystem path and its namespace path. As you probably noticed in the autoload_namespaces file that the namespace is blank for your util package. You’ll probably have to edit your utility class’s composer.json. Instead of:

	"autoload": {
		"psr-0": {
			"": ""

Try changing that to:

    "autoload": {
        "psr-4": {"Problem\\Util\\": ""}

That seem to have fixed it. I really need to read more about this. Having troubles following. Now there is no autoload_namespaces.php line for php-util-lockfile at all and there is nothing about LockFile in the autoload_classmap.php but everything seems to work fine, thanks! :slight_smile: