トレイト
- PHP 5.4、少なくともあなたが使用する前に
- これは、実装するクラスの一部であります
- 彼の外観は、単一のPHPの継承を補うために一種です
ここでは文法のルールです
ベーシックエディション
Doが簡単なデモです
trait Helper{
public function show(){
print "Helper";
}
}
class Example3{
use Helper;
}
$exmaple3=new Example3();
$exmaple3->show();
単にとして定義されtrait
ますが、他のクラス、一つだけ呼び出す必要があるときに、クラスuse
のコースの缶を、名前空間がある場合にも、名前空間を追加する必要があり、。
しかし、注目すべきいくつかの他の点、解決するには、次の11があります。
private
プロパティとメソッドは、アクセスすることもできます
trait Helper{
private function show(){
print "Helper";
}
}
class Example3{
use Helper;
public function showInfo(){
$this->show();
}
}
$exmaple3=new Example3();
$exmaple3->showInfo();
trait
private
この方法は、Example3
で使用することができます。
優先順位
彼らの方法>特色アプローチ>継承されたメソッド
trait Helper{
public function show(){
print "Helper";
}
}
class Base{
public function show(){
print "Base";
}
}
class Example1 extends Base {
use Helper;
public function showInfo(){
$this->show();
}
}
class Example2 extends Base{
use Helper;
public function show(){
print "Example2";
}
}
$exmaple1=new Example1();
$exmaple1->showInfo();
print "\r\n";
$exmaple2=new Example2();
$exmaple2->show();
複数の形質状況
複数の使用trait
を持つクラス、,
ライン上でそれらを結びます。
trait Trait1{
}
trait Trait2{
}
class Examples{
// 使用多个 trait
use Trait1,Trait2;
}
解決の競合
または、いわゆる競合である、上記の例を取るTrait1
とTrait2
同じ方法を定義します。
trait Trait1{
public function show(){
print "Trait1";
}
}
trait Trait2{
public function show(){
print "Trait2";
}
}
class Examples{
// 使用多个 trait
use Trait1,Trait2;
}
PHPは1をされるか分からないん今回はfatel_error
、紛争へのいわゆるソリューションを使用するかを決定することです。
trait Trait1{
public function show(){
print "Trait1";
}
}
trait Trait2{
public function show(){
print "Trait2";
}
}
class Examples{
// 要使用 insteadof (替换)来决定使用哪个方法
use Trait1,Trait2{
Trait2::show insteadof Trait1;
}
}
$examples=new Examples();
$examples->show();
アクセス制御のエイリアスを設定して変更する方法
trait Trait1{
private function show(){
print "Trait1";
}
}
trait Trait2{
private function show(){
print "Trait2";
}
}
class Examples{
use Trait1,Trait2{
// 设置方法别名
Trait1::show as showInfo;
Trait2::show insteadof Trait1;
// 修改访问控制
Trait2::show as public;
}
}
$examples=new Examples();
$examples->show();
次の注意これらのポイント:
- エイリアスを設定することができず、競合を解決する、彼はちょうど別のエイリアスは方法としてそれを設定しました。
- クラス内の1つの方法のためにすることはできません設定エイリアスとアクセス制御を変更します。
特性と特性の組み合わせ
trait Trait1{
public function show(){
print "Trait1";
}
}
trait Trait2{
use Trait1;
public function show(){
print "Trait2";
}
}
class Examples{
use Trait2;
}
$examples=new Examples();
$examples->show();
それは単にすることができているtrait
で呼ばれることtrait
。
抽象メソッドを設定する形質サポート
trait Trait1{
public function show(){
print "Trait1";
}
// 定义了一个抽象方法
abstract function showInfo();
}
trait Trait2{
use Trait1;
public function show(){
print "Trait2";
}
}
class Examples{
use Trait2;
function showInfo()
{
// TODO: Implement showInfo() method.
}
}
$examples=new Examples();
$examples->show();
注意:
trait
組み合わせると、他のは、trait
抽象メソッドを実装する必要はありません、唯一の最後の例では、クラスのものとすることができる唯一の方法を実装する必要があります。
特徴は、静的プロパティを定義します
trait Helper{
static $count=0;
public static function show($message=""){
print "\r\n".$message."\r\n";
}
public function getCount(){
self::$count++;
self::show("count:".self::$count);
}
}
class Example7{
use Helper;
}
class Example8{
use Helper;
}
$example7=new Example7();
$example8=new Example8();
$example7->getCount();
$example8->getCount();
出力:
1
1
私たちはここに呼び出すことを注意が定義されtrait
のstatic $count
、そして使用する2つのクラス許すgetCount
方法を、慣例静的プロパティ、我々はそれを得るだろうと思いました:
1
2
しかし、結果は2つのクラスを呼び出したときに我々が保証することができますので、そうであっても、静的プロパティは、静的プロパティは、お互いに影響を与えないだろう、ではありません。
プロパティ
形質が定義した後、属性クラスは、同じ名前の属性を定義することができない、または致命的なエラーが発生します。例外の種類:互換性の属性は、(同じアクセスの可視性、初期デフォルト値)です。PHP 7.0より前、プロパティに互換性がある、そしてE_STRICTのリマインダーがあるでしょう。