PHP()関数の使用例の説明でSpl_autoload_register

__自動ロード:この機能を見る前に最初に別の関数を見てください。
、__ AUTOLOAD
これは、自動読み込み機能では、PHP5で、我々は未定義のクラスをインスタンス化する場合、この機能を起動します。次の例を見てください:
printit.class.php:

<?php
class PRINTIT { function doPrint() { echo 'hello world'; } } ?> 

index.phpを

<?
function __autoload( $class ) { $file = $class . '.class.php'; if ( is_file($file) ) { require_once($file); } } $obj = new PRINTIT(); $obj->doPrint();?> 

通常出力のHello Worldの実行index.phpを。printitをインスタンス化するとき、自動的に関数を呼び出す__autoload、パラメータの値は、クラス名$クラスはprintitあるindex.phpのでは、何のprintit.class.phpがあっていないので、この時間は、それがprintit.class.phpに導入されました。
この方法は、多くの場合、あまりにも多くの参照を書き込まないよう、だけでなく、システム全体をより柔軟にするために、オブジェクト指向で使用されています。
二、spl_autoload_register()
)(spl_autoload_register を見て、この機能は__autoloadされ、簡単な例を見て、歌で素晴らしい同僚があります。

<?
function loadprint( $class ) { $file = $class . '.class.php'; if (is_file($file)) { require_once($file); } } spl_autoload_register( 'loadprint' ); $obj = new PRINTIT(); $obj->doPrint();?> 

__autoloadはloadprint機能を置き換えます。しかし、自動的に__autoloadようにトリガされませloadprint、その後、spl_autoload_register()役割を果たしていると、それはloadprintの実装にヒットを定義していないPHPクラスを伝えます()。
spl_autoload_register()静的メソッドの呼び出し

<?
class test { public static function loadprint( $class ) { $file = $class . '.class.php'; if (is_file($file)) { require_once($file); } } } spl_autoload_register( array('test','loadprint') ); //另一种写法:spl_autoload_register( "test::loadprint" ); $obj = new PRINTIT(); $obj->doPrint();?> 

spl_autoload_register
(PHP 5> = 5.1.2。)
spl_autoload_register - __autoload()関数は、サイン
記載
ブールspl_autoload_register([コールバックの$ autoload_functionを] )
SPL __autoload関数レジスタスタックの関数です。スタックの機能がアクティブ化されていない場合は、それらを活性化。
あなたのプログラムは__autoload関数を達成した場合、明示的にスタックを__autoloadするために登録する必要があります。spl_autoload_registerので()関数は、Zendエンジン)は(spl_autoloadの__autoload関数()またはspl_autoload_callに置換されているであろう。
パラメータ
autoload_functionは
自動読み込み機能を登録するには。あなたが任意のパラメータを提供しない場合は、自動的に登録されているデフォルトの実装の自動ロード関数spl_autoload()。
値を返す
成功した場合はtrueを返し、失敗した場合にFALSE、。
注:SPLは、標準PHPライブラリ(標準PHPライブラリ)の略です。それは、その主な機能は、自動ロードイテレータ機構を含み、様々なクラスまたはインタフェースを含む達成するために導入された拡張ライブラリPHP5です。SPLオートロード機構は、それらの実装と機能autoload_funcオートロード機能への関数ポインタによって実装され得ます。SPLは、これら2つの異なる機能アドレスにautoload_funcポイントによって異なる自動ローディング機構を達成するために2つの異なる機能のspl_autoload、spl_autoload_callを有しています。

classLOAD
{
 staticfunctionloadClass($class_name)
  {
    $filename= $class_name.".class.php";
 $path= "include/".$filename
    if(is_file($path)) returninclude$path;
  }
}
/**
 * 设置对象的自动载入
 * spl_autoload_register — Register given function as __autoload() implementation
 */
spl_autoload_register(array('LOAD', 'loadClass')); /** *__autoload 方法在 spl_autoload_register 后会失效,因为 autoload_func 函数指针已指向 spl_autoload 方法 * 可以通过下面的方法来把 _autoload 方法加入 autoload_functions list */ spl_autoload_register( '__autoload'); 

両方の方法は、クラスのspl_autoload_registerの__autoloadと機能に登録されている場合、クラスファイルが登録の最初のメソッドまたは関数にロードされたならば、次に、それは、順序の下で登録され、第二の実行が登録されませんメソッドまたは関数クラス。逆に第二登録するメソッドのクラスまたは関数を実行します。

<?php
class autoloader { public static $loader; public static function init() { if (self::$loader == NULL) self::$loader = new self (); return self::$loader; } public function __construct() { spl_autoload_register ( array ($this, 'model' ) ); spl_autoload_register ( array ($this, 'helper' ) ); spl_autoload_register ( array ($this, 'controller' ) ); spl_autoload_register ( array ($this, 'library' ) ); } public function library($class) { set_include_path ( get_include_path () . PATH_SEPARATOR . '/lib/' ); spl_autoload_extensions ( '.library.php' ); spl_autoload ( $class ); } public function controller($class) { $class = preg_replace ( '/_controller$/ui', '', $class ); set_include_path ( get_include_path () . PATH_SEPARATOR . '/controller/' ); spl_autoload_extensions ( '.controller.php' ); spl_autoload ( $class ); } public function model($class) { $class = preg_replace ( '/_model$/ui', '', $class ); set_include_path ( get_include_path () . PATH_SEPARATOR . '/model/' ); spl_autoload_extensions ( '.model.php' ); spl_autoload ( $class ); } public function helper($class) { $class = preg_replace ( '/_helper$/ui', '', $class ); set_include_path ( get_include_path () . PATH_SEPARATOR . '/helper/' ); spl_autoload_extensions ( '.helper.php' ); spl_autoload ( $class ); } } //call autoloader::init (); ?>

おすすめ

転載: www.cnblogs.com/linqingvoe/p/10937709.html