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();
$loader = new Symfony\Component\ClassLoader\ClassLoader