国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php面向對象編程學習筆記

2024-09-04 11:43:37
字體:
來源:轉載
供稿:網友

面向對象編程是php中一種常用的使用方法,本文章來介紹php面向對象簡單使用方法與一些基本知識有需要的朋友可進入參考.

(OOP)來開發.面向對象開發相對于面向過程有很多優點:

維護簡單   模塊化是面向對象編程中的一個特征.實體被表示為類和同一名字空間中具有相同功能的類,我們可以在名字空間中添加一個類而不會影響該名字空間的其他成員.

可擴充性   面向對象編程從本質上支持擴充性.如果有一個具有某種功能的類,就可以很快地擴充這個類,創建一個具有擴充的功能的類.

代碼重用   由于功能是被封裝在類中的,并且類是作為一個獨立實體而存在的,提供一個類庫就非常簡單了.

它比較適合多人合作來開發項目,所以現在很多大中型網站都選擇了用OOP來開發.下面我來介紹面向對象編程類與屬性和方法

PHP中定義類語法格式:

實例代碼如下:

  1. class classname [可選屬性]{ 
  2. public $property [=value];… //用public聲明一個公共標識 然后給予一個變量 變量也可以賦值 
  3. function functionname ( args ){ //類的方法里的成員函數 
  4. 代碼} … 
  5. //類的方法(成員函數) 
  6. 生成對象(類的實例化): $對象名=new classname( ); 

使用對象的屬性

在一個類中,可以訪問一個特殊指針$this當在該類中通過一個操作設置或訪問該變量時,使用$this->name來引用.

實例代碼如下:

  1. class person{ 
  2. function _ _destruct( ) 
  3. echo "bye bye !“; } 
  4. $a=new person(); 

1.final

final:php5新增一個final關鍵字.如果父類中的方法被聲明為final,則子類無法覆蓋該方法;如果一個類被聲明final,則不能被繼承.

實例代碼如下:

  1. class BaseClass{ 
  2.      public function test(){ 
  3.           ehco "test"
  4.      } 
  5.  
  6.      final public function moreTest(){ 
  7.           echo "moretest"
  8.      } 
  9.  
  10. class ChildClass extends BaseClass{ 
  11.      public function moreTest(){ 
  12.           echo "moretest"
  13.      } 
  14. // 產生 Fatal error: Cannot override final method BaseClass::moretest() 

2.__toString(建議用PHP5.2或者更高版本)

實例代碼如下:

  1. class Person{ 
  2.      protected $name
  3.      protected $email
  4.       
  5.      public function setName($name){ 
  6.           $this->name = $name
  7.      } 
  8.  
  9.      public function setEmail($email){ 
  10.           $this->email = $email
  11.      } 
  12.  
  13.      public function __toString(){ 
  14.           return "$this->name <$this->email>"
  15.      } 
  16. $rasums = new Person; 
  17. $rasums->setName('test'); 
  18. $rasums->setEmail('test@qq.com'); 
  19. print $rasums

3.接口和抽象類

接口的作用:你想要保證一個類按照特定的名稱、可見性和原型實現一個或多個方法.

接口的要求:

     類中全部為抽象方法
     抽象方法錢不用加abstract
     接口抽象方法屬性為public
     成員屬性必須為常量

實例代碼如下:

  1. interface ChildTest{ 
  2.      public function childTest(); 
  3. class FathTest implements ChildTest1,ChildTest2{ 
  4.      public function childTest(){ 
  5.           echo 1; 
  6.      } 
  7.      ………… 

抽象的作用: 其實抽象類和接口類有一部分很像,記得在哪里看見這樣一句話,抽象類就把類像的部分抽出來,這句看上去很搞笑,其實它說出了抽象類的真理,抽象類的作用 是,當你發現你的很多類里面用很多方法你不斷的在重復寫,那你就可以考慮使用抽象類了,你可能會說“我不是可以重寫一個類每個公共類我個實例化一個這個公 共類,調用相同的方法就可以了”,這里是可以,實際上抽象類做的工作也就是這個,不過他省去了你實例化的這個步驟,讓你就像直接調用本類方法一樣方便,而 且你還可以重載這個方法.

抽象的要求:

     類中至少有一個抽象方法
     抽象方法錢必須加abstract

實例代碼如下:

  1. abstract class Database{ 
  2.      abstract public function connect(); 
  3.      abstract public function query(); 
  4.      abstract public function fetch(); 
  5.      abstract public function close(); 
  6. 注:抽象方法不能定義為私有方法、不能定義為最終方法,因為它們需要被繼承. 

4.傳遞對象引用

php4:所有“=”都是創建一個副本

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

5.克隆對象

一、聚合類:

__call方法簡介:

當客戶端代碼用類中未定義的方法時,__call會被調用.__call()接受兩個參數,一個是方法名稱,另一個是傳遞給要調用方法的所有參數(包括數組)__call()方法返回的任何值都會返回給客戶,將好像調用方式真實存在一樣

實例代碼如下:

  1. class Address{ 
  2.      protected $city
  3.      protected $country
  4.      public function setCity($city){$this->city = $city;} 
  5.      public function getCity(){return $this->city;} 
  6.      public function setCountry($country){$this->country = $country;} 
  7.      public function getCountry(){return $this->country;} 
  8. class Person{ 
  9.      protected $name
  10.      protected $address
  11.      //淺克隆 
  12.      public function __construct(){ 
  13.           $this->address = new Address; 
  14.      } 
  15.      public function setName($name){ 
  16.           $this->name = $name
  17.      } 
  18.      public function getName(){ 
  19.           return $this->name; 
  20.      } 
  21.      public function __call($method,$arguments){ 
  22.           if(method_exists($this->address,$method)){ 
  23.                return call_user_func_array(array($this->address,$method),$arguments); 
  24.           } 
  25.      } 
  26.      //深克隆 
  27.      public function __clone(){ 
  28.           $this->address = clone $this->address; 
  29.      } 
  30. $test1 = new Person; 
  31. $test2 = clone $test1
  32. $test1->setName('testname1'); 
  33. $test1->setCity('testcity1'); 
  34. $test2->setName('testname2'); 
  35. $test2->setCity('testcity2'); 
  36. echo $test1->getName().'-'.$test1->getCity()."n"
  37. echo $test2->getName().'-'.$test2->getCity()."n"
  38. //testname1-testcity2 
  39. //testname2-testcity2 

6.重要屬性訪問(__set __get __isset __unset) __isset __unset5.1之后才有用

作用:攔截對屬性的需求,為了提高分離的程度,還要實現__isset()和__unset(),以便當我們用isset來檢測屬性或者unset()來刪除屬性,來保證類的行為正確

實例代碼如下:

  1. class Person{ 
  2.      protected $__data = array('email','test'); 
  3.      public function __get($property){ 
  4.           if(isset($this->__data[$property])){ 
  5.                return $this->__data[$property]; 
  6.           }else
  7.                return false; 
  8.           } 
  9.      } 
  10.      public function __set($property,$value){ 
  11.           if(isset($this->__data[$property])){ 
  12.                return $this->__data[$property] = $value
  13.           }else
  14.                return false; 
  15.           } 
  16.      } 
  17.  
  18.      public function __isset($property){ 
  19.           if(isset($this->__data[$property])){ 
  20.                return true; 
  21.           }else
  22.                return false; 
  23.           } 
  24.      } 
  25.  
  26.      public function __unset($property){ 
  27.           if(isset($this->__data[$property])){ 
  28.                return unset($this->__data[$property]); 
  29.           }else
  30.                return false; 
  31.           } 
  32.      } 
  33. $test = new Person; 
  34. $test->email= 'test'
  35. var_dump($test->email); 

注意:

這兩個方法只會捕捉缺少的屬性,如果你為你的類定義了一個屬性,那么當訪問這個屬性時php不會調用__get()和__set();

這兩個方法完全破壞了任何屬性繼承的想法.如果父對象中有個 __get()方法,而你在子類中又實現了自己的__get()方法,那么你的對象不會正確的執行,因為父類的__get()方法永遠不會被調用,當然可以用parent::__get()解決

缺點:

速度相對較慢

使用魔術訪問器方法就不可能在使用反射類,如phpdocumentor這類的工具將代碼自動文檔化,不能將其用于靜態屬性

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 枣强县| 即墨市| 彭山县| 庐江县| 三台县| 溧水县| 游戏| 康马县| 淮安市| 浦北县| 五寨县| 开封县| 长沙县| 闸北区| 孝感市| 尉氏县| 翁牛特旗| 余江县| 沈丘县| 滁州市| 邢台县| 海南省| 克山县| 陕西省| 常山县| 宣汉县| 邵武市| 剑川县| 武邑县| 绵阳市| 遂宁市| 巴中市| 长乐市| 新巴尔虎右旗| 班戈县| 德安县| 德令哈市| 谢通门县| 晋州市| 海安县| 涿鹿县|