|
2> 檢查數(shù)據(jù)類型:
is_array();
is_object();
is_string();
is_null();
is_integer();
3> php5 引入類的類型提示(type hint),用來(lái)約束一個(gè)方法的參數(shù)類型(不是基本數(shù)據(jù)類型,而是類):將類名放在需要約束的方法參數(shù)之前.
例如: function write( ShopProduct $shopProduct){}
4> instanceof 操作符: 如果左邊操作數(shù)的對(duì)象是右邊操作數(shù)所示的類型,結(jié)果為true
例如: if( $shopProduct instanceof BookProduct ) {}
5> 繼承 class son extends parent{}
要調(diào)用父類的方法, 比如構(gòu)造函數(shù),用 parent::__construct();
6> 靜態(tài)方法和屬性
class StaticExample{
static public $a;
static public function hello(){}
}
外部訪問(wèn)使用::
例如: print StaticExample::$a;
內(nèi)部訪問(wèn)使用self::
例如: self::$a;
7> 抽象類, 抽象方法
abstract class xxx{
...
abstract function write(); //沒(méi)有{}
}
抽象類的子類要重新聲明方法并實(shí)現(xiàn)之. 新實(shí)現(xiàn)的方法的訪問(wèn)控制不能比抽象方法的訪問(wèn)控制更嚴(yán)格.
8>接口 interface
只定義功能,不包含實(shí)現(xiàn). 接口中可以包含屬性和方法聲明,但方法體為空;
例如: interface a{
public function b();
}
任何實(shí)現(xiàn)接口的類都要實(shí)現(xiàn)接口中定義的所有方法,否則就必須是抽象類.
類在聲明中使用implements來(lái)實(shí)現(xiàn)某個(gè)接口.
class Shop implements a{
public function b(){
...
}
}
9> 異常 exception
php5引入異常類
10>攔截器 interceptor
__get($property); 訪問(wèn)未定義的屬性時(shí)被調(diào)用
__set($property,$value); 給未定義的屬性賦值時(shí)被調(diào)用
__isset($property); 對(duì)未定義的屬性使用isset()時(shí)被調(diào)用;
__unset($property);對(duì)未定義的屬性調(diào)用unset()時(shí)被調(diào)用;
__call($method, $arg_array); 調(diào)用未定義的方法時(shí)候被調(diào)用
例: __get()的實(shí)現(xiàn)
復(fù)制代碼 代碼如下:
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> 析構(gòu)方法 __destruct()
12> __clone(); 與clone關(guān)鍵字的區(qū)別
class CopyMe();
$first= new CopyMe();
$second=$first;
// php4 : $first和$second是兩個(gè)完全不同的對(duì)象;
// php5: $first和$second指向同一個(gè)對(duì)象
php5中, 對(duì)象的賦值和傳遞都是引用.
如果要拷貝,就要用: $second= clone $first; //現(xiàn)在$first和$second是兩個(gè)完全不同的對(duì)象,(by_value copy)
如果要想控制復(fù)制, 要通過(guò)實(shí)現(xiàn)一個(gè)特殊方法__clone()
13> 自動(dòng)加載: __autoload()
php5引入__autoload()攔截器方法來(lái)自動(dòng)包含類文件.當(dāng)php遇到試圖實(shí)例化一個(gè)未知類的操作時(shí),會(huì)嘗試調(diào)用__autoload()方法,并將類名當(dāng)作字符串參數(shù)傳遞給它.
例如一個(gè)很簡(jiǎn)單的自動(dòng)定位和包含策略:
function __autoload( $classname){
includ_once "$classname.php";
}
====================
14>使用字符串動(dòng)態(tài)引用類
復(fù)制代碼 代碼如下:
$classname="Task";
require_once("tasks/{$classname}.php);
$myObj= new $classname();
$method="getTitle";
$myObj->$method(); //動(dòng)態(tài)方法
15>類函數(shù)和對(duì)象函數(shù)
復(fù)制代碼 代碼如下:
class_exist(); //檢查類是否存在
get_declared_classes(); //獲得當(dāng)前腳本進(jìn)程中定義的所有類(array形式返回)
get_class_methods();//類中所有的public方法列表(array)
method_exist($objname,$method); //對(duì)象或類的方法是否存在
is_callable();//對(duì)象或類的方法不僅存在,且能訪問(wèn)
get_class_vars(); // 屬性
get_parent_class(類或?qū)ο竺Q); //父類
is_subclass_of(); //是否子類,不管接口,接口用 instanceof操作符
16>反射API
由一系列可以分析屬性、方法、類和參數(shù)的內(nèi)置類構(gòu)成,可以動(dòng)態(tài)獲取信息,動(dòng)態(tài)調(diào)用方法.
php技術(shù):PHP面向?qū)ο髮W(xué)習(xí)筆記之一 基礎(chǔ)概念,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。