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

首頁(yè) > 開發(fā) > PHP > 正文

深入分析php之面向?qū)ο?/h1>
2024-05-04 21:53:41
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

以前對(duì)面向?qū)ο?/a>僅限于死記硬背型,工作這么久了,回過(guò)頭來(lái)看又是一翻體悟,供大家看看。
1.final
final:
php5新增一個(gè)final關(guān)鍵字。如果父類中的方法被聲明為final,則子類無(wú)法覆蓋該方法;如果一個(gè)類被聲明final,則不能被繼承。

復(fù)制代碼 代碼如下:www.CuoXIn.com

class BaseClass{
public function test(){
ehco "test";
}

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

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

2.__toString(建議用PHP5.2或者更高版本)
復(fù)制代碼 代碼如下:www.CuoXIn.com

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('test@qq.com');
print $rasums;

3.接口和抽象類
接口的作用:你想要保證一個(gè)類按照特定的名稱、可見性和原型實(shí)現(xiàn)一個(gè)或多個(gè)方法。
接口的要求:
類中全部為抽象方法
抽象方法錢不用加abstract
接口抽象方法屬性為public
成員屬性必須為常量
例:
復(fù)制代碼 代碼如下:www.CuoXIn.com

interface ChildTest{
public function childTest();
}
class FathTest implements ChildTest1,ChildTest2{
public function childTest(){
echo 1;
}
…………
}

抽象的作用: 其實(shí)抽象類和接口類有一部分很像,記得在哪里看見這樣一句話,抽象類就把類像的部分抽出來(lái),這句看上去很搞笑,其實(shí)它說(shuō)出了抽象類的真理,抽象類的作用 是,當(dāng)你發(fā)現(xiàn)你的很多類里面用很多方法你不斷的在重復(fù)寫,那你就可以考慮使用抽象類了,你可能會(huì)說(shuō)“我不是可以重寫一個(gè)類每個(gè)公共類我個(gè)實(shí)例化一個(gè)這個(gè)公 共類,調(diào)用相同的方法就可以了”,這里是可以,實(shí)際上抽象類做的工作也就是這個(gè),不過(guò)他省去了你實(shí)例化的這個(gè)步驟,讓你就像直接調(diào)用本類方法一樣方便,而 且你還可以重載這個(gè)方法。
抽象的要求:
類中至少有一個(gè)抽象方法
抽象方法錢必須加abstract
例:
復(fù)制代碼 代碼如下:www.CuoXIn.com

abstract class Database{
abstract public function connect();
abstract public function query();
abstract public function fetch();
abstract public function close();
}

注:抽象方法不能定義為私有方法、不能定義為最終方法,因?yàn)樗鼈冃枰焕^承。

4.傳遞對(duì)象引用
php4:所有“=”都是創(chuàng)建一個(gè)副本
php5:除了對(duì)象外,其他“=”進(jìn)行賦值時(shí),都是創(chuàng)建一個(gè)副本;而對(duì)象則是引用

5.克隆對(duì)象
一、
聚合類:
__call方法簡(jiǎn)介:
當(dāng)客戶端代碼用類中未定義的方法時(shí),__call會(huì)被調(diào)用。
__call()接受兩個(gè)參數(shù),一個(gè)是方法名稱,另一個(gè)是傳遞給要調(diào)用方法的所有參數(shù)(包括數(shù)組)
__call()方法返回的任何值都會(huì)返回給客戶,將好像調(diào)用方式真實(shí)存在一樣
例:
復(fù)制代碼 代碼如下:www.CuoXIn.com

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.重要屬性訪問(wèn)(__set __get __isset __unset) __isset __unset5.1之后才有用
作用:攔截對(duì)屬性的需求,為了提高分離的程度,還要實(shí)現(xiàn)__isset()和__unset(),以便當(dāng)我們用isset來(lái)檢測(cè)屬性或者unset()來(lái)刪除屬性,來(lái)保證類的行為正確
例:
復(fù)制代碼 代碼如下:www.CuoXIn.com

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);


注意:
這兩個(gè)方法只會(huì)捕捉缺少的屬性,如果你為你的類定義了一個(gè)屬性,那么當(dāng)訪問(wèn)這個(gè)屬性時(shí)php不會(huì)調(diào)用__get()和__set();
這兩個(gè)方法完全破壞了任何屬性繼承的想法。如果父對(duì)象中有個(gè) __get()方法,而你在子類中又實(shí)現(xiàn)了自己的__get()方法,那么你的對(duì)象不會(huì)正確的執(zhí)行,因?yàn)楦割惖腳_get()方法永遠(yuǎn)不會(huì)被調(diào)用,當(dāng)然可以用parent::__get()解決
缺點(diǎn):
速度相對(duì)較慢
使用魔術(shù)訪問(wèn)器方法就不可能在使用反射類,如phpdocumentor這類的工具將代碼自動(dòng)文檔化
不能將其用于靜態(tài)屬性

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

主站蜘蛛池模板: 屏南县| 齐河县| 吉林省| 温宿县| 滨海县| 河源市| 县级市| 普洱| 文安县| 九寨沟县| 永胜县| 金华市| 太保市| 个旧市| 将乐县| 平和县| 元江| 尼木县| 海丰县| 潮安县| 满城县| 嫩江县| 鄄城县| 江西省| 综艺| 永靖县| 三穗县| 江油市| 南昌县| 阜康市| 丹江口市| 平和县| 蕲春县| 花莲市| 朔州市| 象山县| 新闻| 乐山市| 桂东县| 东明县| 富宁县|