SPL_java autoloadD_REGISTER参数问题

急,请各位高人解释一下PHP中的__autoload函数以及spl_autoload_register()函数,我到网上找资料都没看懂_百度知道
急,请各位高人解释一下PHP中的__autoload函数以及spl_autoload_register()函数,我到网上找资料都没看懂
希望不要copy网上的资料,谢谢了
__autoload 常用在自动加载类库处理也就是网上说的 这种方法,根据类名,找出类文件,然后require_onespl_autoload_register()__autoload的最大缺陷是无法有多个autoload方法 好了, 想下下面的这个情景,你的项目引用了别人的一个项目,你的项目中有一个__autoload,别人的项目也有一个__autoload,这样两个__autoload就冲突了。解决的办法就是修改__autoload成为一个,这无疑是非常繁琐的。 因此我们急需使用一个autoload调用堆栈,这样spl的autoload系列函数就出现了。你可以使用spl_autoload_register注册多个自定义的autoload函数 如果你的PHP版本大于5.1的话,你就可以使用spl_autoload
感谢您的回答,但是我脑袋里还是很乱,能不能详细的说说__autoload在什么地方用到,然后spl_autooload_register在什么地方用到?谢谢啦...
__autoload 一个大型点的项目~一个面向对象的项目~你写了很多类,而且一个类以个文件~那是不是你要用这个类的时候需要引用他对把~(require_one)你要引用很多类的时候,就要写很次次require_one而PHP 有__autoload函数你不需要写个脚本在每个脚本开头写一个长长的包含文件列表 __autoload() 函数,它会在试图使用尚未被定义的类时自动调用。意思就是但脚本遇见没有定义的类是就会自动调用他,(没有他就直接报错对把)然后你就可以在__autoload() 函数require_例如API 里很多例子然后spl_autoload_register()我觉得上面说的很清楚了,如果你是面向过程来写的话,我觉得完全没有必要学习他~~
谢谢你给我详细的解答,我现在知道大概的用处了,但是我还不很明白__autoload(),spl_autoload_register()的用法,能不能给我讲解下他们的用法?我看一个类里面给spl_autoload_register()传了一个包含两个参数的数组,但我没看懂,还有一个就是__autoload()能不能直接使用?就是在头部输入__autoload(),然后就可以在下面任意调用类了?
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁spl_autoload_register
(PHP 5 &= 5.1.2)
spl_autoload_register — 注册__autoload()函数
bool spl_autoload_register ([ callback $autoload_function ] )
将函数注册到SPL __autoload函数栈中。如果该栈中的函数尚未激活,则激活它们。
如果在你的程序中已经实现了__autoload函数,它必须显式注册到__autoload栈中。因为
spl_autoload_register()函数会将Zend Engine中的__autoload函数取代为spl_autoload()或
spl_autoload_call()。
autoload_function&
欲注册的自动装载函数。如果没有提供任何参数,则自动注册autoload的默认实现函数
spl_autoload()。
返回值
如果成功则返回 TRUE,失败则返回 FALSE。
注:SPL是Standard PHP Library(标准PHP库)的缩写。它是PHP5引入的一个扩展库,其主要功能包括autoload机制的实现及包括各种Iterator接口或类。 SPL autoload机制的实现是通过将函数指针autoload_func指向自己实现的具有自动装载功能的函数来实现的。SPL有两个不同的函数 spl_autoload, spl_autoload_call,通过将autoload_func指向这两个不同的函数地址来实现不同的自动加载机制。
__autoload
在开发过程中,往往需要在多个脚本文件中用到一些外部函数,这时候每个脚本文件都需要引入一个文件,或者需要在一个脚本文件中使用多个外部函数,这时需要在一个脚本文件中引入多个外部文件,这是就需要在文件首部写很多个require('A.php');或者include('B.php');,这样会显得很烦琐。在php5之后的版本,有了新的特性。在php5中,试图使用尚未定义的类时会自动调用__autoload函数,所以我们可以通过编写__autoload函数来让php自动加载类,而不必写一个长长的包含文件列表。
function __autoload($className) {
require($className.&.php&);
Test t = new Test();//此类在这个文件中未定义,构造对象时会自动调用__autoload函数引入对应的类文件
从这个例子中,我们可以看出autoload至少要做三件事情,第一件事是根据类名确定类文件名,第二件事是确定类文件所在的磁盘路径(在我们的例子是最简单的情况,类与调用它们的PHP程序文件在同一个文件夹下),第三件事是将类从磁盘文件中加载到系统中。第三步最简单,只需要使用include/require即可。要实现第一步,第二步的功能,必须在开发时约定类名与磁盘文件的映射方法,只有这样我们才能根据类名找到它对应的磁盘文件。
因此,当有大量的类文件要包含的时候,我们只要确定相应的规则,然后在__autoload()函数中,将类名与实际的磁盘文件对应起来,就可以实现lazy loading的效果。从这里我们也可以看出__autoload()函数的实现中最重要的是类名与实际的磁盘文件映射规则的实现。
现在有一个这样的问题,如果在一个大项目中,肯定需要用到很多的类库,而这么多的类库,基本上都是由几个不同的人编写的,其类名和实际的磁盘文件的映射规则不会全都相同的。这时候如果要实现自动加载的话就会很复杂,这样的autoload函数会显得十分臃肿。
此时我们可以选择使用spl_autoload_register函数
function redirect($url) {
echo '&script type=&text/javascript& language=&javascript&&window.location.href=&'.$url.'&;&/script&';
spl_autoload_register('redirect');
上面是一个自动加载重定向函数的方法。函数redirect被加入到spl的栈中,这样在项目的其他地方就能通过不引入改文件的地方调用该函数(但是要引入spl_autoload_register()函数所在的脚本文件)。
__autoload 方法在 spl_autoload_register 后会失效,因为 autoload_func 函数指针已指向 spl_autoload 方法&
可以通过下面的方法来把 _autoload 方法加入 autoload_functions list&
spl_autoload_register(&'__autoload'&);&
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:47765次
积分:1354
积分:1354
排名:第12814名
原创:88篇
转载:32篇
(1)(6)(7)(20)(4)(2)(9)(8)(6)(9)(2)(5)(4)(1)(21)(12)(3)除非特别声明,本站所有PHP教程及其他教程/文章均为原创、翻译或网友投稿,版权均归UncleToo中文网所有,
转载请注明作者及出处。原文网址:
读完这篇文章后,你是否有所收获? 分享是一种生活的信念!
&相关阅读:
我来说两句Keyboard Shortcuts?
Next menu item
Previous menu item
Previous man page
Next man page
Scroll to bottom
Scroll to top
Goto homepage
Goto search(current page)
Focus search box
Change language:
Brazilian Portuguese
Chinese (Simplified)
spl_autoload
spl_autoload & Default implementation for __autoload()
Description
void spl_autoload
( string $class_name
[, string $file_extensions = spl_autoload_extensions()
Parameters
class_name
The lowercased name of the class (and namespace) being instantiated.
file_extensions
By default it checks all include paths to
contain filenames built up by the lowercase class name appended by the
filename extensions .inc and .php.
Return Values
No value is returned.
Note, that the default autoload implementation is written in C land and is always slightly faster then your native PHP one.Here is a trick to use default implementation with any configuration:&?php& & define('CLASS_DIR', 'class/')& & set_include_path(get_include_path().PATH_SEPARATOR.CLASS_DIR);& & spl_autoload_extensions('.class.php');& & spl_autoload_register();?&This also works with namespaces out of the box. So you can write code like "use My\Name\Object" and it will map to "class/My/Name/Object.class.php" file path!
Note this function will LOWERCASE the class names its looking for, dont be confused when it cant find Foo_Bar.phpalso, unlike most other autoloader code snippets, this function DOES NOT translate underscores to slashes.class Foo_Bar {}will load foo_bar.php and will not try to load foo/bar.phpYou can get around this withspl_autoload_register(function($class) { return spl_autoload(str_replace('_', '/', $class));});
Note that, the orders of file extensions is important for performance. You should make the priority of your favourite file extension higest or use only one extension for your class files. Check out this example:Some class files:ClassA.php&?php class ClassA { var $val = 'Hello from class "ClassA"'; } ?&ClassB.php&?php class ClassB { var $val = 'Hello from class "ClassB"'; } ?&ClassC.php&?php class ClassC { var $val = 'Hello from class "ClassC"'; } ?&ClassD.php&?php class ClassD { var $val = 'Hello from class "ClassD"'; } ?&ClassE.php&?php class ClassE { var $val = 'Hello from class "ClassE"'; } ?&1. Simple:&?phpfor($n=65; $n&70; $n++) {& & $className = 'Class'.chr($n);& & spl_autoload($className);& & $ins = new $className;& & echo $ins-&val.'&br&';}?&2. Change priority:&?phpspl_autoload_extensions('.php,.inc');for($n=65; $n&70; $n++) {& & $className = 'Class'.chr($n);& & spl_autoload($className);& & $ins = new $className;& & echo $ins-&val.'&br&';}?&Or you can use this simple function that runs a bit faster for the extensions with lower priority :)&?phpfunction my_autoload($className, $extList='.inc,.php') {& & $ext = explode(',',$extList);& & foreach($ext as $x) {& & & & $fname = $className.$x;& & & & if(@file_exists($fname)) {& & & & & & require_once($fname);& & & & & & return true;& & & & }& & }& & return false;}for($n=65; $n&70; $n++) {& & $className = 'Class'.chr($n);& & my_autoload($className);& & $ins = new $className;& & echo $ins-&val.'&br&';}?&---Safak Ozpinar - Istanbul University, Computer Engineering
The documentation is a little unclear when it says: "The lowercased name of the class (and namespace) being instantiated". What it actually means is that the argument can be in whatever case you want, but it will be converted to lowercase before PHP starts looking for files. This is probably because in PHP, class names are case-insensitive (as well as function names and namespaces) so it needs to convert to some canonical format.
One small example that shows how you can use spl_autoload function in your MVC, Framewrk's applications. For example, will use the Loader class. &?php class Loader {& & & & & & protected $_controllerDirectoryPath = array();& & & & protected $_modelDirectoryPath = array();& & & & protected $_libraryDirectoryPath = array();& & & & & & public function __construct()& & {& & & & $this-&modelDirectoryPath& & & = MPATH;& & & & $this-&viewDirectoryPath& & & & = VPATH;& & & & $this-&controllerDirectoryPath = CPATH;& & & & $this-&libraryDirectoryPath& && = LPATH;& & & & & & & & spl_autoload_register(array($this,'load_controller'));& & & & spl_autoload_register(array($this,'load_model'));& & & & spl_autoload_register(array($this,'load_library'));&& & & & & log_message('debug',"Loader Class Initialized");& & }& & public function load_library($library, $param = null)& & {& & & & if (is_string($library)) {& & & & & & return $this-&initialize_class($library);& & & & }& & & & if (is_array($library)) {& & & & & & foreach ($library as $key) {& & & & & & & & return $this-&initialize_class($library);& & & & & & }& & & & }& & & & & & & & & & }& & public function initialize_class($library)& & {& & & & try {& & & & & & if (is_array($library)) {& & & & & & & & foreach($library as $class) {& & & & & & & & & & $arrayObject =& new $class;& & & & & & & & }& & & & & & & & & & & & & & return $this;& & & & & & }& & & & & & if (is_string($library)) {& & & & & & & & $stringObject = new $library;& & & & & & }else {& & & & & & & & throw new ISException('Class name must be string.');& & & & & & }& & & & & & if (null == $library) {& & & & & & & & throw new ISException('You must enter the name of the class.');& & & & & & }& & & & } catch(Exception $exception) {& & & & & & echo $exception;& & & & }& & }& & & & & & public function load_controller($controller)& & {& & & & if ($controller) {& & & & & & set_include_path($this-&controllerDirectoryPath);& & & & & & spl_autoload_extensions('.php');& & & & & & spl_autoload($class);& & & & }& & }& & & & & & & public function load_models($model)& & {& & & & if ($model) {& & & & & & set_include_path($this-&modelDirectoryPath);& & & & & & spl_autoload_extensions('.php');& & & & & & spl_autoload($class);& & & & }& & }& & & & & & & public function load_library($library)& & {& & & & if ($library) {& & & & & & set_include_path($this-&libraryDirectoryPath);& & & & & & spl_autoload_extensions('.php');& & & & & & spl_autoload($class);& & & & }& & }& & & &
&?php
spl_autoload_extensions('.class.php');
spl_autoload_register('loadClasses');
function loadClasses($className)
& & if( file_exists(ROOT_DIR.DS.'controller/'.$className.'.class.php' ) ){
& & & & set_include_path(ROOT_DIR.DS.'controller'.DS);
& & & & spl_autoload($className);
& & }
& & elseif( file_exists('model/'.$className.'.class.php' ) ){
& & & & set_include_path(ROOT_DIR.DS.'model'.DS);
& & & & spl_autoload($className);
& & }elseif( file_exists('view/'.$className.'.class.php' ) ){
& & & & set_include_path(ROOT_DIR.DS.'view'.DS);
& & & & spl_autoload($className& & );
& & }else
& & {
& & & & set_include_path(ROOT_DIR.DS.'lib'.DS);
& & & & spl_autoload($className& & );
& & }
Mine is simplier, and without include/require:function autoload($className) {& & set_include_path('./library/classes/');& & spl_autoload($className); //replaces include/require}spl_autoload_extensions('.class.php');spl_autoload_register('autoload');
If you want to make the best use out of autoload with an APC cache don't use spl_autoload. It uses relative paths and thus will perform a stat even with apc.stat=0 (either that, or it doesn't work at all).Instead make a custom function and use require/include with an absolute path (register it with spl_autoload_register).Do NOT use *_once functions or a relative path. This will fail harder than spl_autoload.Also avoid using file_exists and is_file. This will also perform a stat.Why are stats bad? Because they access the file system. PHP does have a stat cache that helps, but it defeats the purpose of apc.stat = 0.It's also good to keep in mind that it's good to keep your custom autoload function simple. This is my Loader class:&?phpclass Loader{& & & & public static function registerAutoload()& & {& & & & return spl_autoload_register(array(__CLASS__, 'includeClass'));& & }& & & & public static function unregisterAutoload()& & {& & & & return spl_autoload_unregister(array(__CLASS__, 'includeClass'));& & }& & & & public static function includeClass($class)& & {& & & & require(PATH . '/' . strtr($class, '_\\', '//') . '.php');& & }}?&Also want to point out that APC does an optimization with require/include (not *_once) with relative paths if require/include is done in the global scope (and isn't conditional). So it would be a good idea to explicitly include files you know you're going to use on every request (but don't use *_once). You could, for example, add a "registerProfiledAutoload" to the above class and keep track of what you're including to help you determine what you could explicitly include (during development, not production). The key is try not to make heavy use out of autoload.If you must use relative paths and don't care about having to lower-case your file-names then spl_autoload works great.
Just thought I'd react to simast at gmail dot com's note: While he has a point in saying C outperforms PHP, his suggestion is micro-optimization. I'm not 100% against micro-optimizing code, but if you do, go all the way:&?php& & define('CLASS_DIR', 'class/')& & set_include_path(get_include_path().PATH_SEPARATOR.CLASS_DIR);This adds the include path to THE END of the paths PHP will scan for the class file, resulting in a bunch of misses (file-not-found's) before actually looking into the CLASS_DIR.A more sensible approach, then would be to write& & set_include_path(& & & & CLASS_DIR.& & & & PATH_SEPARATOR,& & & & get_include_path()& & );}

我要回帖

更多关于 spl autoload call 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信