Sql_autoload_register()函数

Spl_autoload_register()  注册__autoload()函数

将函数注册到SPL __autoload函数栈中。如果该栈中的函数尚未激活,则激活它们。

说明

bool spl_autoload_register ([ callback $autoload_function ] )

__autoload函数取代为spl_autoload()或spl_autoload_call()。

参数

autoload_function

欲注册的自动装载函数。如果没有提供任何参数,则自动注册autoload的默认实现函数 spl_autoload()

 

范例一

 

 1 <?php
 2 
 3 namespace Foobar;
 4 class Foo {
 5 
 6     static public function test($name) {
 7         print '[['. $name .']]';
 8 
 9     }
10 }
11 
12 spl_autoload_register(__NAMESPACE__ .'::Foo::test'); // As of PHP 5.3.0
13 new InexistentClass;
14 
15 ?>

 

 

以上例程的输出类似于:

[[Foobar::InexistentClass]]
Fatal error: Class 'Foobar::InexistentClass' not found in ...

参见:

假如你的目录机构为"/abc/def/ghi",你的index.php位于最顶层,但是你想要使用命名空间"def"或者"ghi",你可以切换命名空间到根目录使用set_include_path(__DIR__.'/abc')并且使用sql_autoload_register()方法不带任何参数;

<?php

Class Autoloader
{
       public static function Register() {
      
               return spl_autoload_register(array('autoloader','load'));
      }    

       public static function load($strObjectName) {

               if(class_exists($strObjectName) === false) {
                         
                         return false;
               }
               
               $strObjectFilePath = BASE_PATH . $strObjectName . '.php';
              if(file_exists($strObjectFilePath) === false || is_readable($strObjectFilePath ) === false) {

                      return false;    
               }    
              require ($strObjectFilePath);    
       }
}
?>

  

posted on 2016-03-10 22:19  队伍  阅读(1220)  评论(0)    收藏  举报

导航