PHP面向?qū)ο髮W(xué)習(xí)筆記之一 基礎(chǔ)概念
更新時(shí)間:2012年10月06日 22:33:29 作者:
PHP面向?qū)ο髮W(xué)習(xí)筆記之一 基礎(chǔ)概念,想要學(xué)習(xí)PHP面向?qū)ο缶幊痰呐笥芽梢詤⒖枷?/div>
1> if( "false" ) 等效于 if( true), 因?yàn)榉强兆址莟rue
2> 檢查數(shù)據(jù)類型:
is_array();
is_object();
is_string();
is_null();
is_integer();
3> PHP5 引入類的類型提示(type hint),用來約束一個(gè)方法的參數(shù)類型(不是基本數(shù)據(jù)類型,而是類):將類名放在需要約束的方法參數(shù)之前.
例如: function write( ShopProduct $shopProduct){}
4> instanceof 操作符: 如果左邊操作數(shù)的對象是右邊操作數(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(){}
}
外部訪問使用::
例如: print StaticExample::$a;
內(nèi)部訪問使用self::
例如: self::$a;
7> 抽象類, 抽象方法
abstract class xxx{
...
abstract function write(); //沒有{}
}
抽象類的子類要重新聲明方法并實(shí)現(xiàn)之. 新實(shí)現(xiàn)的方法的訪問控制不能比抽象方法的訪問控制更嚴(yán)格.
8>接口 interface
只定義功能,不包含實(shí)現(xiàn). 接口中可以包含屬性和方法聲明,但方法體為空;
例如: interface a{
public function b();
}
任何實(shí)現(xiàn)接口的類都要實(shí)現(xiàn)接口中定義的所有方法,否則就必須是抽象類.
類在聲明中使用implements來實(shí)現(xiàn)某個(gè)接口.
class Shop implements a{
public function b(){
...
}
}
9> 異常 exception
PHP5引入異常類
10>攔截器 interceptor
__get($property); 訪問未定義的屬性時(shí)被調(diào)用
__set($property,$value); 給未定義的屬性賦值時(shí)被調(diào)用
__isset($property); 對未定義的屬性使用isset()時(shí)被調(diào)用;
__unset($property);對未定義的屬性調(diào)用unset()時(shí)被調(diào)用;
__call($method, $arg_array); 調(diào)用未定義的方法時(shí)候被調(diào)用
例: __get()的實(shí)現(xiàn)
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è)完全不同的對象;
// PHP5: $first和$second指向同一個(gè)對象
PHP5中, 對象的賦值和傳遞都是引用.
如果要拷貝,就要用: $second= clone $first; //現(xiàn)在$first和$second是兩個(gè)完全不同的對象,(by_value copy)
如果要想控制復(fù)制, 要通過實(shí)現(xiàn)一個(gè)特殊方法__clone()
13> 自動加載: __autoload()
PHP5引入__autoload()攔截器方法來自動包含類文件.當(dāng)PHP遇到試圖實(shí)例化一個(gè)未知類的操作時(shí),會嘗試調(diào)用__autoload()方法,并將類名當(dāng)作字符串參數(shù)傳遞給它.
例如一個(gè)很簡單的自動定位和包含策略:
function __autoload( $classname){
includ_once "$classname.php";
}
====================
14>使用字符串動態(tài)引用類
$classname="Task";
require_once("tasks/{$classname}.php);
$myObj= new $classname();
$method="getTitle";
$myObj->$method(); //動態(tài)方法
15>類函數(shù)和對象函數(shù)
class_exist(); //檢查類是否存在
get_declared_classes(); //獲得當(dāng)前腳本進(jìn)程中定義的所有類(array形式返回)
get_class_methods();//類中所有的public方法列表(array)
method_exist($objname,$method); //對象或類的方法是否存在
is_callable();//對象或類的方法不僅存在,且能訪問
get_class_vars(); // 屬性
get_parent_class(類或?qū)ο竺Q); //父類
is_subclass_of(); //是否子類,不管接口,接口用 instanceof操作符
16>反射API
由一系列可以分析屬性、方法、類和參數(shù)的內(nèi)置類構(gòu)成,可以動態(tài)獲取信息,動態(tài)調(diào)用方法.
2> 檢查數(shù)據(jù)類型:
is_array();
is_object();
is_string();
is_null();
is_integer();
3> PHP5 引入類的類型提示(type hint),用來約束一個(gè)方法的參數(shù)類型(不是基本數(shù)據(jù)類型,而是類):將類名放在需要約束的方法參數(shù)之前.
例如: function write( ShopProduct $shopProduct){}
4> instanceof 操作符: 如果左邊操作數(shù)的對象是右邊操作數(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(){}
}
外部訪問使用::
例如: print StaticExample::$a;
內(nèi)部訪問使用self::
例如: self::$a;
7> 抽象類, 抽象方法
abstract class xxx{
...
abstract function write(); //沒有{}
}
抽象類的子類要重新聲明方法并實(shí)現(xiàn)之. 新實(shí)現(xiàn)的方法的訪問控制不能比抽象方法的訪問控制更嚴(yán)格.
8>接口 interface
只定義功能,不包含實(shí)現(xiàn). 接口中可以包含屬性和方法聲明,但方法體為空;
例如: interface a{
public function b();
}
任何實(shí)現(xiàn)接口的類都要實(shí)現(xiàn)接口中定義的所有方法,否則就必須是抽象類.
類在聲明中使用implements來實(shí)現(xiàn)某個(gè)接口.
class Shop implements a{
public function b(){
...
}
}
9> 異常 exception
PHP5引入異常類
10>攔截器 interceptor
__get($property); 訪問未定義的屬性時(shí)被調(diào)用
__set($property,$value); 給未定義的屬性賦值時(shí)被調(diào)用
__isset($property); 對未定義的屬性使用isset()時(shí)被調(diào)用;
__unset($property);對未定義的屬性調(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è)完全不同的對象;
// PHP5: $first和$second指向同一個(gè)對象
PHP5中, 對象的賦值和傳遞都是引用.
如果要拷貝,就要用: $second= clone $first; //現(xiàn)在$first和$second是兩個(gè)完全不同的對象,(by_value copy)
如果要想控制復(fù)制, 要通過實(shí)現(xiàn)一個(gè)特殊方法__clone()
13> 自動加載: __autoload()
PHP5引入__autoload()攔截器方法來自動包含類文件.當(dāng)PHP遇到試圖實(shí)例化一個(gè)未知類的操作時(shí),會嘗試調(diào)用__autoload()方法,并將類名當(dāng)作字符串參數(shù)傳遞給它.
例如一個(gè)很簡單的自動定位和包含策略:
function __autoload( $classname){
includ_once "$classname.php";
}
====================
14>使用字符串動態(tài)引用類
復(fù)制代碼 代碼如下:
$classname="Task";
require_once("tasks/{$classname}.php);
$myObj= new $classname();
$method="getTitle";
$myObj->$method(); //動態(tài)方法
15>類函數(shù)和對象函數(shù)
復(fù)制代碼 代碼如下:
class_exist(); //檢查類是否存在
get_declared_classes(); //獲得當(dāng)前腳本進(jìn)程中定義的所有類(array形式返回)
get_class_methods();//類中所有的public方法列表(array)
method_exist($objname,$method); //對象或類的方法是否存在
is_callable();//對象或類的方法不僅存在,且能訪問
get_class_vars(); // 屬性
get_parent_class(類或?qū)ο竺Q); //父類
is_subclass_of(); //是否子類,不管接口,接口用 instanceof操作符
16>反射API
由一系列可以分析屬性、方法、類和參數(shù)的內(nèi)置類構(gòu)成,可以動態(tài)獲取信息,動態(tài)調(diào)用方法.
您可能感興趣的文章:
- PHP學(xué)習(xí)筆記(二) 了解PHP的基本語法以及目錄結(jié)構(gòu)
- PHP學(xué)習(xí)筆記(一) 簡單了解PHP
- PHP 面向?qū)ο蟪绦蛟O(shè)計(jì)(oop)學(xué)習(xí)筆記 (五) - PHP 命名空間
- PHP 面向?qū)ο蟪绦蛟O(shè)計(jì)(oop)學(xué)習(xí)筆記 (四) - 異常處理類Exception
- PHP 面向?qū)ο蟪绦蛟O(shè)計(jì)(oop)學(xué)習(xí)筆記(三) - 單例模式和工廠模式
- PHP 面向?qū)ο蟪绦蛟O(shè)計(jì)(oop)學(xué)習(xí)筆記 (二) - 靜態(tài)變量的屬性和方法及延遲綁定
- PHP 面向?qū)ο蟪绦蛟O(shè)計(jì)(oop)學(xué)習(xí)筆記(一) - 抽象類、對象接口、instanceof 和契約式編程
- PHP學(xué)習(xí)筆記之字符串編碼的轉(zhuǎn)換和判斷
- php cookie使用方法學(xué)習(xí)筆記分享
- PHP Switch 語句之學(xué)習(xí)筆記
- php之CodeIgniter學(xué)習(xí)筆記
- php之Memcache學(xué)習(xí)筆記
- php學(xué)習(xí)筆記之面向?qū)ο缶幊?/a>
- PHP面向?qū)ο髮W(xué)習(xí)筆記之二 生成對象的設(shè)計(jì)模式
- 兩千行代碼的PHP學(xué)習(xí)筆記匯總
相關(guān)文章
PHP不使用內(nèi)置函數(shù)實(shí)現(xiàn)字符串轉(zhuǎn)整型的方法示例
一般php字符串類型的數(shù)字如果想轉(zhuǎn)成整型的數(shù)字,我們都是采用系統(tǒng)內(nèi)置的API去做轉(zhuǎn)換,但下面這篇文章主要給大家介紹了關(guān)于PHP不使用內(nèi)置函數(shù)實(shí)現(xiàn)字符串轉(zhuǎn)整型的方法示例,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-07-07Ajax+Jpgraph實(shí)現(xiàn)的動態(tài)折線圖功能示例
這篇文章主要介紹了Ajax+Jpgraph實(shí)現(xiàn)的動態(tài)折線圖功能,結(jié)合實(shí)例形式分析了ajax結(jié)合jpgraph.php類庫繪制動態(tài)折線圖的相關(guān)操作技巧,需要的朋友可以參考下2019-02-02PHP syntax error, unexpected $end 錯(cuò)誤的一種原因及解決
PHP 遇到 syntax error, unexpected $end 錯(cuò)誤時(shí),查錯(cuò)思路其實(shí)還是看看文件里 PHP 的開始標(biāo)記和結(jié)束標(biāo)記是否配對,還要額外注意注釋里是否出現(xiàn)過 ?> 喲。2008-10-10