PHP鍛藉悕绌洪棿涓庤嚜鍔ㄥ姞杞界被璇﹁В
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP鍛藉悕绌洪棿涓庤嚜鍔ㄥ姞杞界被璇﹁В相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%8a%80%e5%b7%a7%e6%80%bb%e7%bb%93' title='鎶€宸ф€荤粨'>鎶€宸ф€荤粨
澶х 鍩烘湰 static array 鍔犺浇 娉ㄦ剰 string 璋冪敤鏈枃瀹炰緥璁茶堪浜哖HP鍛藉悕绌洪棿涓庤嚜鍔ㄥ姞杞界被銆傚垎浜粰澶у渚涘ぇ瀹跺弬鑰冿紝鍏蜂綋濡備笅锛?br>
浠婂ぉ鎴戣缁欏ぇ瀹朵粙缁嶇殑鏄疨HP鐨勫懡鍚嶇┖闂?鍜?鑷姩鍔犺浇绫?br>
鎴戝厛绠€鍗曠殑鍒嗗紑婕旂ず 鍦ㄦ斁鍦ㄤ竴璧?br>
澶у璇风湅锛?br>浠€涔堟槸鑷姩鍔犺浇绫伙紵
鎯冲繀澶у閮藉簲璇ョ煡閬揰_autoload 杩欎釜榄旀湳鏂规硶
$db = new DB();
function __autoload($className)
{
echo $className;
exit();
//鍦ㄨ繖涓噷闈㈠紩鐢ㄧ被
}
鍦ㄨ繍鐢ㄨ繖涓被鍙兘鍔犺浇涓€娆?锛屽湪寰堝鏃跺€欐垜浠寮曞叆鐨勪笉姝竴涓被杩欎釜鏃跺€欐€庝箞鍔炲憿锛?br>
鐗涢€肩殑php澶х浠?nbsp; 鍑轰簡涓柊鏂规硶 鏉ヨВ鍐?杩欎釜闂 璇风湅灏辨槸杩欎釜鍑芥暟
spl_autoload_register() 婊¤冻浜嗘绫婚渶姹傘€?瀹冨疄闄呬笂鍒涘缓浜?autoload 鍑芥暟鐨勯槦鍒楋紝鎸夊畾涔夋椂鐨勯『搴忛€愪釜鎵ц銆傜浉姣斾箣涓嬶紝 __autoload() 鍙彲浠ュ畾涔変竴娆°€?br>
杩欎釜鍑芥暟鐨勭敤娉曟湁寰堝 杩欓噷鍏堢粰澶у浠嬬粛涓€绉嶄娇鐢ㄦ柟娉?br>
濡備笅锛?br>
function load1($className)
{
echo 1;
require $className . 鈥?php鈥?
}
spl_autoload_register(鈥榣oad1鈥?; //灏唋oad1鍑芥暟娉ㄥ唽鍒拌嚜鍔ㄥ姞杞介槦鍒椾腑銆?br>$db = new DB(); //鎵句笉鍒癉B绫伙紝灏变細鑷姩鍘昏皟鐢ㄥ垰娉ㄥ唽鐨刲oad1鍑芥暟浜?br>//php 5.3涔嬪悗锛屼篃鍙互鍍忚繖鏍锋敮鎸佸尶鍚嶅嚱鏁颁簡銆?br>spl_autoload_register(function($className){
if (is_file(鈥?/lib/鈥?. $className . 鈥?php鈥?) {
require 鈥?/lib/鈥?. $className . 鈥?php鈥?
}
});
澶氫釜spl_autoload_register鐨勪娇鐢?br>
鍒ゆ柇鏄惁鏈夋鏂囦欢
濡傛灉鏈夊紩鍏?濡傛灉娌℃湁 娌℃湁濡傛灉浜嗭紝鍝潵鐨勫鏋?br>
function load1($className)
{
echo 1;
if (is_file($className . 鈥?php鈥?) {
require $className . 鈥?php鈥?
}
}
function load2($className)
{
echo 2;
if (is_file(鈥?/app/鈥?. $className . 鈥?php鈥?) {
require 鈥?/app/鈥?. $className . 鈥?php鈥?
}
}
function __autoload($className)
{
echo 3;
if (is_file(鈥?/lib/鈥?. $className . 鈥?php鈥?) {
require 鈥?/lib/鈥?. $className . 鈥?php鈥?
}
}
//娉ㄥ唽浜?涓?br>spl_autoload_register(鈥榣oad1鈥?;
spl_autoload_register(鈥榣oad2鈥?;
spl_autoload_register(鈥榑_autoload鈥?;
$db = new DB(); //DB灏卞湪鏈洰褰曚笅
$info = new Info(); //Info 鍦?lib/Info.php
鎴戜滑鍙互鎵撳嵃spl_autoload_functions()鍑芥暟锛屾潵鏄剧ず涓€鍏辨敞鍐屼簡澶氬皯涓嚜鍔ㄥ姞杞斤細
var_dump(spl_autoload_functions());
//鏁扮粍鐨勫舰寮忚緭鍑?br>array (size=3)
0 => string 鈥榣oad1鈥?(length=5)
1 => string 鈥榣oad2鈥?(length=5)
2 => string 鈥榑_autoload鈥?(length=10)
鍛藉悕绌洪棿
涓€涓猵hp鏂囦欢閲?鏄笉鏀寔 new 涓や釜鍚嶅瓧鐨?br>
杩欎釜鏃跺€欐垜浠氨鑳界敤鐨勫埌鍛藉悕绌洪棿浜?br>
namespace app;
class new{
public function ten()
{
echo "85";
}
}
namespace hii;
class new{
public function ten()
{
echo "85";
}
}
spl_autoload_register鑷姩鍔犺浇+namespace鍛藉悕绌洪棿 鐨勪娇鐢?br>
搴熻瘽涓嶅鏁?nbsp; 鐪嬩緥瀛?br>
AutoLoadingloading
<?php
namespace AutoLoading;
class loading {
public static function autoload($className)
{
//鎶? 杞崲灞?/ , 渚夸簬鍏煎Linux鏂囦欢鎵俱€俉indows 涓嬫槸閫氱敤鐨?br> //鐢变簬namspace 寰堣鏍硷紝鎵€浠ョ洿鎺ュ緢蹇氨鑳芥壘鍒?br> $fileName = str_replace(鈥榎鈥? 鈥?鈥? DIR . 鈥榎鈥? $className) . 鈥?php鈥?
if (is_file($fileName)) {
require $fileName;
} else {
echo $fileName . 鈥?is not exist鈥? die;
}
}
}
涓婇潰灏辨槸涓€涓嚜鍔ㄥ姞杞界殑鏍稿績鎬濇兂鏂规硶銆備笅闈㈡垜浠氨鏉pl_autoload_register鏉ユ敞鍐岃繖涓嚱鏁帮細
index.php
<?php
//瀹氫箟褰撳墠鐨勭洰褰曠粷瀵硅矾寰?br>define(鈥楧IR鈥? dirname(__FILE__));
//鍔犺浇杩欎釜鏂囦欢
require DIR . 鈥?loading.php鈥?
//閲囩敤`鍛藉悕绌洪棿`鐨勬柟寮忔敞鍐屻€俻hp 5.3 鍔犲叆鐨?br>//涔熷繀椤绘槸寰楁槸static闈欐€佹柟娉曡皟鐢紝鐒跺悗灏卞儚鍔犺浇namespace鐨勬柟寮忚皟鐢紝娉ㄦ剰锛氫笉鑳戒娇鐢╱se
spl_autoload_register("\AutoLoading\loading::autoload");
// 璋冪敤涓変釜namespace绫?br>//瀹氫綅鍒癓ib鐩綍涓嬬殑Name.php
LibName::test();
//瀹氫綅鍒癆pp鐩綍涓婣ndroid鐩綍涓嬬殑Name.php
AppandroidName::test();
//瀹氫綅鍒癆pp鐩綍涓婭os鐩綍涓嬬殑Name.php
AppiosName::test();
鐢变簬鎴戜滑鏄噰鐢≒SR-O鏂瑰紡鏉ュ畾涔塶amespace鐨勫懡鍚嶇殑锛屾墍浠ュ緢濂界殑瀹氫綅鍒拌繖涓枃浠剁殑鍦ㄥ摢涓洰褰曚笅浜嗐€傚緢鐖姐€傚涓嶅銆?br>
APPAndroidName
namespace AppAndroid;
class Name
{
public function __construct()
{
echo __NAMESPACE__ . "<br>";
}
public static function test()
{
echo __NAMESPACE__ . 鈥?static function test <br>鈥?
}
}
鎵€浠ュ氨浼氬緢瀹规槗鎵惧埌鏂囦欢锛屽苟杈撳嚭锛?br>
Lib static function test
AppAndroid static function test
AppIos static function test
濂戒簡銆傚熀鏈殑涓滆タ灏辫瀹屼簡.
鏇村鍏充簬PHP鐩稿叧鍐呭鎰熷叴瓒g殑璇昏€呭彲鏌ョ湅鏈珯涓撻锛氥€妏hp闈㈠悜瀵硅薄绋嬪簭璁捐鍏ラ棬鏁欑▼銆嬨€併€奝HP鍩烘湰璇硶鍏ラ棬鏁欑▼銆嬨€併€奝HP杩愮畻涓庤繍绠楃鐢ㄦ硶鎬荤粨銆嬨€併€奝HP缃戠粶缂栫▼鎶€宸ф€荤粨銆嬨€併€奝HP鏁扮粍(Array)鎿嶄綔鎶€宸уぇ鍏ㄣ€嬨€併€妏hp瀛楃涓?string)鐢ㄦ硶鎬荤粨銆嬨€併€妏hp+mysql鏁版嵁搴撴搷浣滃叆闂ㄦ暀绋嬨€嬪強銆妏hp甯歌鏁版嵁搴撴搷浣滄妧宸ф眹鎬汇€?br>
甯屾湜鏈枃鎵€杩板澶уPHP绋嬪簭璁捐鏈夋墍甯姪銆?/p>
以上是关于PHP鍛藉悕绌洪棿涓庤嚜鍔ㄥ姞杞界被璇﹁В的主要内容,如果未能解决你的问题,请参考以下文章
Intellij鎵撳寘jar鏂囦欢锛屸€渏ava.lang.SecurityException: Invalid signature file digest for Manifest main att