PHP物件導向學習筆記之一 基礎概念_PHP教程

來源:互聯網
上載者:User
1> if( "false" ) 等效於 if( true), 因為非Null 字元串是true
2> 檢查資料類型:
is_array();
is_object();
is_string();
is_null();
is_integer();
3> PHP5 引入類的類型提示(type hint),用來約束一個方法的參數類型(不是基礎資料型別 (Elementary Data Type),而是類):將類名放在需要約束的方法參數之前.
例如: function write( ShopProduct $shopProduct){}

4> instanceof 操作符: 如果左邊運算元的對象是右邊運算元所示的類型,結果為true
例如: if( $shopProduct instanceof BookProduct ) {}

5> 繼承 class son extends parent{}
要調用父類的方法, 比如建構函式,用 parent::__construct();

6> 靜態方法和屬性
class StaticExample{
static public $a;
static public function hello(){}
}
外部存取使用::
例如: print StaticExample::$a;
內部訪問使用self::
例如: self::$a;

7> 抽象類別, 抽象方法
abstract class xxx{
...
abstract function write(); //沒有{}
}

抽象類別的子類要重新聲明方法並實現之. 新實現的方法的存取控制不能比抽象方法的存取控制更嚴格.

8>介面 interface
只定義功能,不包含實現. 介面中可以包含屬性和方法聲明,但方法體為空白;
例如: interface a{
public function b();
}
任何實現介面的類都要實現介面中定義的所有方法,否則就必須是抽象類別.
類在聲明中使用implements來實現某個介面.
class Shop implements a{
public function b(){
...
}
}

9> 異常 exception
PHP5引入異常類

10>攔截器 interceptor
__get($property); 訪問未定義的屬性時被調用
__set($property,$value); 給未定義的屬性賦值時被調用
__isset($property); 對未定義的屬性使用isset()時被調用;
__unset($property);對未定義的屬性調用unset()時被調用;
__call($method, $arg_array); 調用未定義的方法時候被調用
例: __get()的實現
複製代碼 代碼如下:
function __get($property){
$method="get{$property}";
if(method_exists($this,$method)){
return $this->$method();
}
}

function getName(){ return "Bob";}

function __isset($property){
$method="get{$porperty}";
return(method_exists($this, $method));
}

function __set($property, $value){
$method="set{$property}";
if( method_exists($this,$method)){
return $this->$method($value);
}
}


11> 析構方法 __destruct()

12> __clone(); 與clone關鍵字的區別
class CopyMe();
$first= new CopyMe();
$second=$first;
// PHP4 : $first和$second是兩個完全不同的對象;
// PHP5: $first和$second指向同一個對象
PHP5中, 對象的賦值和傳遞都是引用.
如果要拷貝,就要用: $second= clone $first; //現在$first和$second是兩個完全不同的對象,(by_value copy)
如果要想控制複製, 要通過實現一個特殊方法__clone()

13> 自動載入: __autoload()
PHP5引入__autoload()攔截器方法來自動包含類檔案.當PHP遇到試圖執行個體化一個未知類的操作時,會嘗試調用__autoload()方法,並將類名當作字串參數傳遞給它.
例如一個很簡單的自動定位和包含策略:
function __autoload( $classname){
includ_once "$classname.php";
}

====================
14>使用字串動態引用類
複製代碼 代碼如下:
$classname="Task";
require_once("tasks/{$classname}.php);
$myObj= new $classname();
$method="getTitle";
$myObj->$method(); //動態方法

15>類函數和對象函數
複製代碼 代碼如下:
class_exist(); //檢查類是否存在
get_declared_classes(); //獲得當前指令碼進程中定義的所有類(array形式返回)
get_class_methods();//類中所有的public方法列表(array)
method_exist($objname,$method); //對象或類的方法是否存在
is_callable();//對象或類的方法不僅存在,且能訪問
get_class_vars(); // 屬性
get_parent_class(類或對象名稱); //父類
is_subclass_of(); //是否子類,不管介面,介面用 instanceof操作符

16>反射API
由一系列可以分析屬性、方法、類和參數的內建類構成,可以動態擷取資訊,動態調用方法.

http://www.bkjia.com/PHPjc/326065.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/326065.htmlTechArticle1 if( "false" ) 等效於 if( true), 因為非Null 字元串是true 2 檢查資料類型: is_array(); is_object(); is_string(); is_null(); is_integer(); 3 PHP5 引入類的類型提示...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.