A3 Code Note

11/30/2022

#12 Theme Development | traits in php | php traits tutorial

traits

Earlier we could only inherit properties and Functions from one class to another, by extending them
以前は、プロパティと関数を継承することによって、あるクラスから別のクラスに継承することしかできませんでした。

If we want the properties/methods of inferited class in another, we need to extend it again – creates chain of inheritance
継承されたクラスのプロパティとメソッドを別に持ちたい場合は、再度拡張する必要があります。具体的には継承のチェーンを作成します。

PHP( 5.4+ ) introduced a mechanism for code reusability called traits
そのれを解消するため、PHP5.4以降は、traitsと呼ばれるコードを再利用できるメカニズムを導入しました。

例 ——

Teacherーextend→ Person
teach()
Studentーtrait→ Person
learn()
Student2ーtrait→ Person
Student3ーtrait→ Person

Let’s say there is a Teacher class which will be your parent class which has got some of the methods, for example teach().
例えば、Teacherクラスがあって、それがteachメソッドを持つ親クラスになるとします。

And we have a Person class and I want my Person class to have the method and properties of teacher.
Personクラスがあり、そのPersonクラスにTeacherにあるメソッドやプロパティを持たせたいとします。

So all I have to do is just extend it
その場合Teacherを拡張させるだけで良いです。

Let’s say we have another class which is Student and it has a method call learn() like Teacher has a method called teach().
次に、Studentというクラスがあり、Teacherにteach()メソッドがあったように、Studentにもlearn()というメソッドがあったとします。

If I want to use learn method from Student which is a different class altogether.
もしStudentからlearnメソッドを使いたいのなら、それは全く別のクラスです。

Because now we’ll have trades in 5.4 and above versions of php I can just use the trait.
php5.4以降のバージョンだとtraitを使用するだけで良いです。

And I can include those features.
そうするとこれらの機能を含めることができます。

Student class can actually provide this method to multiple classes
Studentクラスは実際にこのメソッドを複数のクラスで使うことができます。

And one class can actually use the trait of different classes.
それにより、とあるクラスは異なるクラスの特性を使用できます。

In this case if we have Student it can get the method of Student if you have another Student.
この場合、Studentがあったら他のStudentからメソッドを使うことができます。

trait allow us to reuse sets of methods freely in several independent classes living in different classes hierarchies.
traitは異なるクラス階層に存在するいくつかの独立したクラス、メソッドのセットを自由に再利用できます。

Share functionalities from multiple classes
複数のクラスの機能を共有することができます。

A trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way
traitはクラスに似ていますが、きめ細かく一貫した方法で機能をグループ化することのみを目的としています。

It’s not possible to instantiate a trait on its own.
traitを単独でインスタンス化することはできません。

#11の index.phpを以下のように変更

<?php

trait Say_World {
    public function say_hello() {
        echo 'Hello Trait';
    }
}

class Base {
    use Say_World;

    public function __construct() {

    }
}

$base = new Base();
$base->say_hello();

Hello Trait
と返ってくるはずです。
以下のように変更すると、

<?php

trait Say_World {
    public function say_hello() {
        echo 'Hello Trait';
    }
}

class Teacher {
    public function say_name() {
        echo 'Teacher';
    }
}

class Base extends Teacher {
    use Say_World;

    public function __construct() {

    }
}

$base = new Base();
$base->say_hello();
$base->say_name();

Hello TraitTeacher
と返ってきます。