概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:42:30 | 140 次阅读 | 评论: 0 | 来源: 网络整理

Introduction

Finding the file for a particular class can be an expensive task. Luckily, the ClassLoader component comes with two classes to cache the mapping from a class to its containing file. Both the ApcClassLoader and the XcacheClassLoader wrap around an object which implements a findFile() method to find the file for a class.

注解

Both the ApcClassLoader and the XcacheClassLoader can be used to cache Composer’s autoloader.

ApcClassLoader

ApcClassLoader wraps an existing class loader and caches calls to its findFile() method using APC:

require_once '/path/to/src/Symfony/Component/ClassLoader/ApcClassLoader.php';

// instance of a class that implements a findFile() method, like the ClassLoader
$loader = ...;

// 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();

XcacheClassLoader

XcacheClassLoader uses XCache to cache a class loader. Registering it is straightforward:

require_once '/path/to/src/Symfony/Component/ClassLoader/XcacheClassLoader.php';

// instance of a class that implements a findFile() method, like the ClassLoader
$loader = ...;

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

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

// deactivate the original, non-cached loader if it was registered previously
$loader->unregister();
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务