如何使用 Symfony Class 加载器
how to use Symfony Class Loader
我有以下结构
api
普通
配置
文档
脚本
供应商
我将自定义自动加载器放在 common/php/autoload.php 下,其中包含
<?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();
正如您所知,composer 将所有内容都放在 ROOTDIR/vendor 文件夹下。
Error I get is Fatal error: Uncaught Error: Class 'ClassLoader' not found in ROOTDIR/common/php/autoload.php:7
更新
当我尝试在 common/ccp 下加载自定义 class 时,它没有加载。
Fatal error: Uncaught Error: Class 'CCP\Notification' not found in ROOTDIR/scripts/cli-test-email.php:12
class
的内容
<?php
namespace CCP
class Notification{
...
}
更新 2
我的脚本在 script
文件夹下。如果我添加 use
它不会出错,但就回声或任何错误而言没有任何反应。如果我删除 use
它找不到 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();
对此的了解:
use Symfony\ClassLoader\ClassLoader;
$loader = new \ClassLoader();
你应该做:
$loader = new Symfony\ClassLoader\ClassLoader();
或:
use Symfony\ClassLoader\ClassLoader;
$loader = new ClassLoader(); //no backslash at the begining
当你在classname前面加反斜杠的时候,就是根命名空间,所以前面加use
也没关系,因为这里不用。
例如,假设您有两个 类 同名 SomeClass
。其中一个在root命名空间下,另一个在Some/Namespace/SomeClass
下
现在:
use Some/Namespace/SomeClass;
$object1 = new SomeClass(); //this is Some/Namespace/SomeClass instace
$object2 = new \SomeClass(); //this is SomeClass from root namespace.
编辑:
至于您更新后的问题中的问题 - 它可能与区分大小写有关。尝试使您的目录名称与命名空间匹配,包括区分大小写。
我有以下结构
api
普通
配置
文档
脚本
供应商
我将自定义自动加载器放在 common/php/autoload.php 下,其中包含
<?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();
正如您所知,composer 将所有内容都放在 ROOTDIR/vendor 文件夹下。
Error I get is Fatal error: Uncaught Error: Class 'ClassLoader' not found in ROOTDIR/common/php/autoload.php:7
更新
当我尝试在 common/ccp 下加载自定义 class 时,它没有加载。
Fatal error: Uncaught Error: Class 'CCP\Notification' not found in ROOTDIR/scripts/cli-test-email.php:12
class
的内容<?php
namespace CCP
class Notification{
...
}
更新 2
我的脚本在 script
文件夹下。如果我添加 use
它不会出错,但就回声或任何错误而言没有任何反应。如果我删除 use
它找不到 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();
对此的了解:
use Symfony\ClassLoader\ClassLoader;
$loader = new \ClassLoader();
你应该做:
$loader = new Symfony\ClassLoader\ClassLoader();
或:
use Symfony\ClassLoader\ClassLoader;
$loader = new ClassLoader(); //no backslash at the begining
当你在classname前面加反斜杠的时候,就是根命名空间,所以前面加use
也没关系,因为这里不用。
例如,假设您有两个 类 同名 SomeClass
。其中一个在root命名空间下,另一个在Some/Namespace/SomeClass
现在:
use Some/Namespace/SomeClass;
$object1 = new SomeClass(); //this is Some/Namespace/SomeClass instace
$object2 = new \SomeClass(); //this is SomeClass from root namespace.
编辑:
至于您更新后的问题中的问题 - 它可能与区分大小写有关。尝试使您的目录名称与命名空间匹配,包括区分大小写。