程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 深入分析php之面向對象

深入分析php之面向對象

編輯:關於PHP編程

以前對面向對象僅限於死記硬背型,工作這麼久了,回過頭來看又是一翻體悟,供大家看看。
1.final
final:php5新增一個final關鍵字。如果父類中的方法被聲明為final,則子類無法覆蓋該方法;如果一個類被聲明final,則不能被繼承。
復制代碼 代碼如下:
class BaseClass{
     public function test(){
          ehco "test";
     }

     final public function moreTest(){
          echo "moretest";
     }
}

class ChildClass extends BaseClass{
     public function moreTest(){
          echo "moretest";
     }
}
// 產生 Fatal error: Cannot override final method BaseClass::moretest()

2.__toString(建議用PHP5.2或者更高版本)
復制代碼 代碼如下:
class Person{
     protected $name;
     protected $email;

     public function setName($name){
          $this->name = $name;
     }

     public function setEmail($email){
          $this->email = $email;
     }

     public function __toString(){
          return "$this->name <$this->email>";
     }
}
$rasums = new Person;
$rasums->setName('test');
$rasums->setEmail('[email protected]');
print $rasums;

3.接口和抽象類
接口的作用:你想要保證一個類按照特定的名稱、可見性和原型實現一個或多個方法。
接口的要求:
     類中全部為抽象方法
     抽象方法錢不用加abstract
     接口抽象方法屬性為public
     成員屬性必須為常量
例:
復制代碼 代碼如下:
interface ChildTest{
     public function childTest();
}
class FathTest implements ChildTest1,ChildTest2{
     public function childTest(){
          echo 1;
     }
     …………
}

抽象的作用: 其實抽象類和接口類有一部分很像,記得在哪裡看見這樣一句話,抽象類就把類像的部分抽出來,這句看上去很搞笑,其實它說出了抽象類的真理,抽象類的作用 是,當你發現你的很多類裡面用很多方法你不斷的在重復寫,那你就可以考慮使用抽象類了,你可能會說“我不是可以重寫一個類每個公共類我個實例化一個這個公 共類,調用相同的方法就可以了”,這裡是可以,實際上抽象類做的工作也就是這個,不過他省去了你實例化的這個步驟,讓你就像直接調用本類方法一樣方便,而 且你還可以重載這個方法。
抽象的要求:
     類中至少有一個抽象方法
     抽象方法錢必須加abstract
例:
復制代碼 代碼如下:
abstract class Database{
     abstract public function connect();
     abstract public function query();
     abstract public function fetch();
     abstract public function close();
}

注:抽象方法不能定義為私有方法、不能定義為最終方法,因為它們需要被繼承。

4.傳遞對象引用
php4:所有“=”都是創建一個副本
php5:除了對象外,其他“=”進行賦值時,都是創建一個副本;而對象則是引用

5.克隆對象
一、
聚合類:
__call方法簡介:
當客戶端代碼用類中未定義的方法時,__call會被調用。
__call()接受兩個參數,一個是方法名稱,另一個是傳遞給要調用方法的所有參數(包括數組)
__call()方法返回的任何值都會返回給客戶,將好像調用方式真實存在一樣
例:
復制代碼 代碼如下:
class Address{
     protected $city;
     protected $country;

     public function setCity($city){$this->city = $city;}
     public function getCity(){return $this->city;}
     public function setCountry($country){$this->country = $country;}
     public function getCountry(){return $this->country;}
}

class Person{
     protected $name;
     protected $address;
     //淺克隆
     public function __construct(){
          $this->address = new Address;
     }

     public function setName($name){
          $this->name = $name;
     }
     public function getName(){
          return $this->name;
     }

     public function __call($method,$arguments){
          if(method_exists($this->address,$method)){
               return call_user_func_array(array($this->address,$method),$arguments);
          }
     }
     //深克隆
     public function __clone(){
          $this->address = clone $this->address;
     }
}

$test1 = new Person;
$test2 = clone $test1;

$test1->setName('testname1');
$test1->setCity('testcity1');
$test2->setName('testname2');
$test2->setCity('testcity2');

echo $test1->getName().'-'.$test1->getCity()."\n";
echo $test2->getName().'-'.$test2->getCity()."\n";
//testname1-testcity2
//testname2-testcity2

6.重要屬性訪問(__set __get __isset __unset) __isset __unset5.1之後才有用
作用:攔截對屬性的需求,為了提高分離的程度,還要實現__isset()和__unset(),以便當我們用isset來檢測屬性或者unset()來刪除屬性,來保證類的行為正確
例:
復制代碼 代碼如下:
class Person{
     protected $__data = array('email','test');

     public function __get($property){
          if(isset($this->__data[$property])){
               return $this->__data[$property];
          }else{
               return false;
          }
     }

     public function __set($property,$value){
          if(isset($this->__data[$property])){
               return $this->__data[$property] = $value;
          }else{
               return false;
          }
     }

     public function __isset($property){
          if(isset($this->__data[$property])){
               return true;
          }else{
               return false;
          }
     }

     public function __unset($property){
          if(isset($this->__data[$property])){
               return unset($this->__data[$property]);
          }else{
               return false;
          }
     }
}

$test = new Person;
$test->email= 'test';
var_dump($test->email);

注意:
     這兩個方法只會捕捉缺少的屬性,如果你為你的類定義了一個屬性,那麼當訪問這個屬性時php不會調用__get()和__set();
     這兩個方法完全破壞了任何屬性繼承的想法。如果父對象中有個 __get()方法,而你在子類中又實現了自己的__get()方法,那麼你的對象不會正確的執行,因為父類的__get()方法永遠不會被調用,當然可以用parent::__get()解決
缺點:
     速度相對較慢
     使用魔術訪問器方法就不可能在使用反射類,如phpdocumentor這類的工具將代碼自動文檔化
     不能將其用於靜態屬性

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved