標籤:order span cti 方式 聲明 static ora UI 自己
1.Final關鍵字
含義:最終的,最後的
作用:
1.如果父類中的方法被聲明為 final,則子類無法覆蓋該方法。
如果一個類被聲明為 final,則不能被繼承。
2.屬性不能被定義為 final,只有類和方法才能被定義為 final。
用法:直接在類或者方法前加final關鍵字。
---------------------------------------------------------------------------
2.類的自動載入
含義:
1.解決代碼重複,把重複的代碼寫在一個檔案夾中,讓其他PHP檔案調用自動載入。
2.解決PHP檔案過多 include等引入PHP過於龐大造成負擔。
實現函數:
spl_autoload_register() 函數:可以註冊任意數量的自動載入器。
__autoload():嘗試載入未定義的類 也能自動載入類和介面(不推薦,可能被棄用)。(注意:處理大配量序時使程式變得臃腫,無法實現,代碼變複雜,給未來的系統維護和系統效率帶來很大的負面影響)
注意:自動載入不可用於 PHP 的 CLI 互動模式。
php負載檔案方式:
1、include,include_once,requice,requice_one常規載入
2、__autoload()
3、spl_autoload_register()
__autoload()自動載入
<?php function __autoload($class){ $file = $class . ‘.php‘;//拼接路徑 if (is_file($file)) { //判斷$file是否存在 require_once($file); //從$file路徑匯入一次 } } $a = new A();注意:由於__autoload()是個函數,只能存在一次。-----------------------------------------------------------------------------spl_autoload_register()自動載入 spl_autoload_register() 它實際上建立了 autoload 函數的隊列,按定義時的順序逐個執行。相比之下, __autoload() 只可以定義一次。<?php function loader($class){ $file = $class . ‘.php‘; if (is_file($file)) { require_once($file); } } spl_autoload_register(‘loader‘); $a = new A(); 直接使用匿名函數:<?php spl_autoload_register(function($file){ $file = $class . ‘.php‘; if (is_file($file)) { require_once($file); }}); $a = new A();
這樣子也是可以正常啟動並執行,這時候php在尋找類的時候就沒有調用__autoload而是調用我們自己定義的函數loader了。同樣的道理,下面這種寫法也是可以的:
| 12345678910111213 |
<?php class Loader { public static function loadClass($class){ $file = $class . ‘.php‘; if (is_file($file)) { require_once($file); } } } spl_autoload_register(array(‘Loader‘, ‘loadClass‘)); //spl_autoload_register(array(__CLASS__, ‘loadClass‘)); //spl_autoload_register(array($this, ‘loadClass‘)); $a = new A(); |
PHP物件導向——Final關鍵字 類的自動載入