shorif2000
shorif2000

Reputation: 2654

how to use Symfony Class Loader

I have the following structure

api

common

config

docs

scripts

vendor

I put custom autoloader under common/php/autoload.php which contains

<?php
require_once BASE_DIR . 'vendor/autoload.php';
require_once BASE_DIR . 'vendor/symfony/class-loader/ClassLoader.php';

use Symfony\ClassLoader\ClassLoader;

$loader = new \ClassLoader();

// to enable searching the include path (eg. for PEAR packages)
$loader->setUseIncludePath(true);

// ... register namespaces and prefixes here - see below

$loader->register();

// register a single namespaces
$loader->addPrefix('CCP', BASE_DIR . 'common/ccp/');


// cache files locations
require_once BASE_DIR . 'vendor/symfony/class-loader/ApcClassLoader.php';

// sha1(__FILE__) generates an APC namespace prefix
$cachedLoader = new ApcClassLoader(sha1(__FILE__), $loader);

// register the cached class loader
$cachedLoader->register();

// deactivate the original, non-cached loader if it was registered previously
$loader->unregister();

As you may know composer places everything under ROOTDIR/vendor folder.

Error I get is Fatal error: Uncaught Error: Class 'ClassLoader' not found in ROOTDIR/common/php/autoload.php:7

UPDATE

When I try to load my custom class under common/ccp it does not load.

Fatal error: Uncaught Error: Class 'CCP\Notification' not found in ROOTDIR/scripts/cli-test-email.php:12

contents of class

<?php
namespace CCP

class Notification{
...
}

UPDATE 2

My scripts is under script folder. If I add the use it doesn't error but nothing happens in terms of echo or any errors. If I remove the use it doesn't find the class Notification.

#!/opt/SP/php-7.0.10/bin/php -q
<?php
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(E_ALL|E_STRICT);
echo "before use";
use CCP/Notification;
echo "after use";

$notification = new Notification();

print_r($notification);
$notification->emailTest();

Upvotes: 2

Views: 927

Answers (1)

Jakub Matczak
Jakub Matczak

Reputation: 15656

Indtead of this:

use Symfony\ClassLoader\ClassLoader;

$loader = new \ClassLoader();

You should do either:

$loader = new Symfony\ClassLoader\ClassLoader();

or:

use Symfony\ClassLoader\ClassLoader;

$loader = new ClassLoader(); //no backslash at the begining

When you put a backslash in front of classname, it means the root namespace, so it doesn't matter that you put use before, because it's not used here.

As an example let's assume you have two classes with the same name SomeClass. One of them is under root namespace, and the another is under Some/Namespace/SomeClass

Now:

use Some/Namespace/SomeClass;

$object1 = new SomeClass(); //this is Some/Namespace/SomeClass instace
$object2 = new \SomeClass(); //this is SomeClass from root namespace.

EDIT:

As for your issue in your updated question - it may be related to case sensitivity. Try to make your directories names match namespace, including case sensitivity.

Upvotes: 1

Related Questions