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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

php traits 使用,php中traits的使用

發(fā)布時(shí)間:2023/12/19 php 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php traits 使用,php中traits的使用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

php是單繼承語(yǔ)言,但是如果想在一個(gè)class中實(shí)現(xiàn)多繼承的話(huà),可以使用traits代替.

關(guān)于使用.使用關(guān)鍵字trait: 從某個(gè)方面,可以理解為class,但是不能實(shí)例化

trait中可以定義public,protected,private方法/屬性,且父類(lèi)中如果使用trait,子類(lèi)也會(huì)繼承trait,甚至private方法/屬性也能繼承

trait?Hello{

public?function?sayH(){

echo?'hello?';

}

protected?function?sayW(){

echo?'world';

}

private?function?pri(){

echo?'this?is?a?private?function';

}

static?public?function?wowo(){

echo?'this?is?a?static?function';

}

public?function?tst(){

echo?'this?is?trait';

}

}

class?one{

use?Hello;

public?function?say(){

$this->pri();

}

public?function?tst(){

echo?'this?is?one';

}

}

class?two?extends?one{

public?function?tst(){

echo?'this?is?two';

}

}

//偷懶,報(bào)錯(cuò)的方法也假設(shè)可以執(zhí)行下去

Hello::wowo();????????//this?is?a?static?function

$human?=?new?one();

$human->sayH();????????//hello

$human->sayW();????????//此處報(bào)錯(cuò),調(diào)用保護(hù)方法

$human->pri();????????//此處報(bào)錯(cuò),調(diào)用私有方法

$human->tst();????????//this?is?one

one::wowo();????????//this?is?a?static?function

$human->say();????????//可以?xún)?nèi)部調(diào)用?this?is?a?private?function

$people?=?new?two();

$people->sayH();????//hello

$people->sayW();????//報(bào)錯(cuò),調(diào)用保護(hù)方法

$people->pri();????//此處報(bào)錯(cuò),調(diào)用私有方法???!注意,私有屬性繼承下來(lái)了

two::wowo();????????//this?is?a?static?function

$people->say();????//內(nèi)部調(diào)用?this?is?a?private?function

$people->tst();????//this?is?two

?>

可以發(fā)現(xiàn),trait 中的 私有屬性 也可以繼續(xù)下來(lái),可以理解為,two把use Hello 也繼承下來(lái)了,而且如果子類(lèi)中有同名方法,則會(huì)覆蓋父類(lèi)和trait中的方法

trait中還可以使用抽象類(lèi)

trait?Hello{

abstract?public?function?say();

}

class?one{

use?Hello;

public?function?say(){

}

}

?>

當(dāng)class one中不存在say方法時(shí),便會(huì)報(bào)錯(cuò).

當(dāng)有多個(gè)trait時(shí),使用,隔開(kāi),或者使用復(fù)合traits

trait?Hello{

}

trait?World{

}

trait?HW{

use?Hello,World;

}

class?one{

use?Hello,World;

}

class?two{

use?HW;

}

?>

以上one 跟 two的實(shí)現(xiàn)效果是一樣的;

當(dāng)兩個(gè)trait鐘的方法名一樣時(shí),需要用關(guān)鍵字insteadof指定使用哪個(gè),不然就會(huì)報(bào)錯(cuò),如果兩個(gè)都想保留,則可以用as通過(guò)別名來(lái)實(shí)現(xiàn),同時(shí)as也可以實(shí)現(xiàn)對(duì)方法權(quán)限的更改

trait?H{

public?function?A(){

echo?'HA';

}

public?function?B(){

echo?'HB';

}

}

trait?W{

public?function?A(){

echo?'WA';

}

public?function?B(){

echo?'WB';

}

}

trait?Z{

public?function?A(){

echo?'ZA';

}

}

//若沒(méi)有use后{}里的說(shuō)明,則會(huì)報(bào)錯(cuò)

//Trait?method?A?has?not?been?applied,?because?there?are?collisions?with?other?trait?methods?on?HW

trait?HW{

use?H,W{

H::A?insteadof?W,Z;

W::B?insteadof?H;??//W中的B方法取代H中的B方法

Z::A?as?C;

A?as?protected;

}

}

class?one{

use?HW;

public?function?say(){

$this->A();

}

}

$a?=?new?one();

$a->say();????????//HA

$a->A();????????//因?yàn)楸桓淖兞藱?quán)限,所以無(wú)法在外部調(diào)用

echo?'
';

$a->B();????????//WB

echo?'
';

$a->C();????????//ZA

?>

參考: http://blog.csdn.net/longlongmylove/article/details/7521379

總結(jié)

以上是生活随笔為你收集整理的php traits 使用,php中traits的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。