PHP の基本 Autoload_PHP チュートリアル

WBOY
リリース: 2016-07-13 10:37:19
オリジナル
1039 人が閲覧しました

PHP の自動ロード メカニズムは非常に重要です。ここでは 2 つの小さな演習を示します。

元の記事、転載する場合はその旨を明記してください: http://www.cnblogs.com/phpgcs
ファイル構造は次のとおりです。自動読み込みを実現するには2つの方法があります
1、カスタム関数
2、spl_autoload_register()
liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ ll ./*
-rw-rw-r-- 1 liuyuan liuyuan 800 2月19日 11:39 ./func_autoload.php
-rw-rw-r-- 1 liuyuan liuyuan 906 2月19日 11:28 ./spl_autoload.php
./含む:
合計16
drwxrwxr-x 2 liuyuan liuyuan 4096 2月19日 11:42 ./
drwxrwxr-x 3 liuyuan liuyuan 4096 2月19日 11:43 ../
-rw-rw-r-- 1 liuyuan liuyuan 142 2月19日 11:42 aClass.php
-rw-rw-r-- 1 liuyuan liuyuan 143 2月19日 11:42 bClass.php
まずカスタム関数メソッドを見てみましょう:
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '');
print_r(get_include_files());
エコー EOL;
print get_include_path();
エコー EOL;
//set_include_path(get_include_path().PATH_SEPARATOR.'/var/www/ly_php/php_spl/include/');
//set_include_path(dirname(__FILE__).'/include');
//set_include_path(dirname(__FILE__).'/include/');
関数 __autoload($className){
$filename = './include/'.$className.'.php';
//$filename = './include/'.$className.'.php';
//$filename = '/var/www/ly_php/php_spl/include/'.$className.'.php';
if(file_exists($filename)){
include_once $filename;
}その他{
exit('ファイルなし');
}
}
$a = 新しい aClass();
$b = 新しい bClass();
print_r(get_include_files());
?>
実行結果は次のとおりです:
+ コードを表示
2 番目の方法:
クラスmyLoader{
パブリック静的関数 autoload($className){
$filename = './include/'.$className.'.php';
if(file_exists($filename)){
include_once $filename;
}その他{
exit('ファイルなし');
}
}
}
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '
');
spl_autoload_register(array('myLoader', 'autoload'));
/**
autoload_func 関数ポインタがすでに spl_autoload メソッドを指しているため、*__autoload メソッドは spl_autoload_register の後は無効になります
* 次のメソッドを通じて、_autoload メソッドを autoload_functions リストに追加できます
*/
//spl_autoload_register( '__autoload' );
error_reporting(E_ALL^E_NOTICE^E_WARNING^E_ERROR);
error_reporting(E_NOTICE | E_WARNING);
$a = 新しい aClass();
print_r(get_include_files());
エコー EOL;
$b = 新しい bClass();
エコー EOL;
?>

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/735880.html技術記事 PHP の自動ロード メカニズムは非常に重要です。ここに 2 つの小さな演習とオリジナルの記事があります。転載する場合はその旨を明記してください。 http://www.cnblogs.com/phpgcs ファイル構造は次のとおりです。自動ロードを実現するには 2 つの方法があります。 ..
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!