2

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();
5
  • $loader = new Symfony\Component\ClassLoader\ClassLoader Commented Apr 18, 2017 at 8:49
  • why do I have to include `Symfony\Component\ClassLoader`? Commented Apr 18, 2017 at 8:55
  • Things you may want look up: how namespaces works and what's psr-0/4 Commented Apr 18, 2017 at 9:04
  • The symfony class loader is depreciated in 3.3 and will be going away in 4.0. I can't think of any reason why you would want to use it. Might be looking at some very old tutorials? And yes once you have required vendor/autoload.php then you need not require anything else. symfony.com/doc/current/components/class_loader.html Commented Apr 18, 2017 at 12:13
  • @Cerad I put comment on below answer. So I am using turotial jessesnet.com/development-notes/2014/php-composer-autoloading which worked for me. is this up to date? Commented Apr 19, 2017 at 9:12

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I renamed common/ccp to common/CCP but still same issue
I couldn't get this to work plus i see symfony autoload is being depreciated so I used composer instead which is working for me jessesnet.com/development-notes/2014/php-composer-autoloading

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.