PHPのspl_autoload_register()関数の使い方の詳しい説明
この記事では、主に PHP の spl_autoload_register() 関数の使用法を紹介し、__autoload 関数と spl_autoload_register 関数の関連使用スキルを例の形式で分析します。自習マニュアル
この関数を理解する前に、別の関数 __autoload を見てみましょう。1. __autoload
printit.class.php:
<?php class PRINTIT { function doPrint() { echo 'hello world'; } } ?>
<? function __autoload( $class ) { $file = $class . '.class.php'; if ( is_file($file) ) { require_once($file); } } $obj = new PRINTIT(); $obj->doPrint();?>
2. spl_autoload_register()
spl_autoload_register() をもう一度見てください。簡単な例を見てみましょう:<? function loadprint( $class ) { $file = $class . '.class.php'; if (is_file($file)) { require_once($file); } } spl_autoload_register( 'loadprint' ); $obj = new PRINTIT(); $obj->doPrint();?>
<? 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() 関数を登録します
説明
bool spl_autoload_register ([ コールバック $autoload_function ] )関数を SPL __autoload 関数スタックに登録します。このスタック内の関数がまだアクティブでない場合はアクティブにします。 __autoload 関数がプログラムに実装されている場合は、__autoload スタックに明示的に登録する必要があります。 spl_autoload_register() 関数は、Zend Engine の __autoload 関数を spl_autoload() または spl_autoload_call() に置き換えるためです。
Parameters
登録するオートロード関数。パラメータを指定しない場合は、autoload のデフォルト実装関数 spl_autoload() が自動的に登録されます。
戻り値
成功した場合は TRUE を返し、失敗した場合は FALSE を返します。注:
SPL は Standard PHP Library の略称です。これは、PHP5 で導入された拡張ライブラリであり、その主な機能には、オートロード メカニズムとさまざまな Iterator インターフェイスまたはクラスの実装が含まれます。 SPL オートロード メカニズムは、関数ポインタ autoload_func をオートロード関数を持つ自己実装関数にポイントすることによって実装されます。 SPL には 2 つの異なる関数 spl_autoload と spl_autoload_call があり、autoload_func をこれら 2 つの異なる関数アドレスに指定することで、異なる自動ロード メカニズムが実装されます。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');
<?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 (); ?>
おすすめ関連記事:
1
.PHPオートロードメカニズムの紹介 - spl_autoload_register()関数、PHPクラスの自動ロード
2.spl_autoload_registerを使用して自動ロードの例を実装する方法の詳細な説明
関連ビデオの推奨: 1. Dugu Jiu 格安(4)_PHP ビデオチュートリアル
以上がPHPのspl_autoload_register()関数の使い方の詳しい説明の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









PHP 8.4 では、いくつかの新機能、セキュリティの改善、パフォーマンスの改善が行われ、かなりの量の機能の非推奨と削除が行われています。 このガイドでは、Ubuntu、Debian、またはその派生版に PHP 8.4 をインストールする方法、または PHP 8.4 にアップグレードする方法について説明します。

CakePHP でデータベースを操作するのは非常に簡単です。この章では、CRUD (作成、読み取り、更新、削除) 操作について理解します。

ファイルのアップロードを行うには、フォーム ヘルパーを使用します。ここではファイルアップロードの例を示します。

CakePHP は、PHP 用のオープンソース フレームワークです。これは、アプリケーションの開発、展開、保守をより簡単にすることを目的としています。 CakePHP は、強力かつ理解しやすい MVC のようなアーキテクチャに基づいています。モデル、ビュー、コントローラー

CakePHP へのログインは非常に簡単な作業です。使用する関数は 1 つだけです。 cronjob などのバックグラウンド プロセスのエラー、例外、ユーザー アクティビティ、ユーザーが実行したアクションをログに記録できます。 CakePHP でのデータのログ記録は簡単です。 log()関数が提供されています

Visual Studio Code (VS Code とも呼ばれる) は、すべての主要なオペレーティング システムで利用できる無料のソース コード エディター (統合開発環境 (IDE)) です。 多くのプログラミング言語の拡張機能の大規模なコレクションを備えた VS Code は、
