日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php面向对象的概括图解,深入分析php之面向对象

發布時間:2024/10/8 php 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php面向对象的概括图解,深入分析php之面向对象 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本篇文章是對php中的面向對象進行了詳細的分析介紹,需要的朋友參考下

以前對面向對象僅限于死記硬背型,工作這么久了,回過頭來看又是一翻體悟,供大家看看。

1.finalfinal: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 email>";

}

}

$rasums = new Person;

$rasums->setName('test');

$rasums->setEmail('test@qq.com');

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這類的工具將代碼自動文檔化

不能將其用于靜態屬性

總結

以上是生活随笔為你收集整理的php面向对象的概括图解,深入分析php之面向对象的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。