Home  >  Article  >  Backend Development  >  Simple examples of using traits in PHP_PHP tutorial

Simple examples of using traits in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:53:461084browse

A simple example of using traits in PHP

This article mainly introduces a simple example of using traits in PHP. This article focuses on the syntax of traits, what functions traits have, and what situations they are. Use traits below, friends in need can refer to it

Traits in PHP 5.4 is a newly introduced feature. I really don’t know how to translate it accurately into Chinese. Its actual purpose is to use multiple inheritance in some situations, but PHP does not have multiple inheritance, so such a thing was invented.

Traits can be understood as a set of methods that can be called by different classes, but Traits are not classes! They cannot be instantiated. Let’s take an example to look at the syntax first:

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

trait myTrait{

function traitMethod1(){}

function traitMethod2(){}

}

//然后是调用这个traits,语法为:

class myClass{

use myTrait;

}

//这样就可以通过use myTraits,调用Traits中的方法了,比如:

$obj = new myClass();

$obj-> traitMethod1 ();

$obj-> traitMethod2 ();

>

1

2

3

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

// Class Client

class Client {

private $address;

public getAddress() {

return $this->address;

}

public setAddress($address) {

$this->address = $address;

}

}

 

class Business extends Client{

//这里可以使用address属性

}

 

// Class Individual

class Individual extends Client{

//这里可以使用address属性

}

4 5 6 7 8 9 10 11 12 13 14 15 16 17
<🎜>trait myTrait{<🎜> <🎜>function traitMethod1(){}<🎜> <🎜>function traitMethod2(){}<🎜> <🎜> <🎜> <🎜>}<🎜> <🎜> <🎜> <🎜>//Then call this traits, the syntax is: <🎜> <🎜>class myClass{<🎜> <🎜>use myTrait;<🎜> <🎜>}<🎜> <🎜> <🎜> <🎜>//In this way, you can call the methods in Traits through use myTraits, such as: <🎜> <🎜>$obj = new myClass();<🎜> <🎜>$obj-> traitMethod1 (); $obj-> traitMethod2 (); >
Next, let’s explore why traits are used. For example, there are two classes, business and Individual. They both have address attributes. The traditional approach is to Abstract a parent class that both classes have common characteristics, such as client, and set the access attributes address, business and individual in the client class to inherit them respectively, as shown in the following code:  ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // Class Client class Client { private $address; public getAddress() { return $this->address; } public setAddress($address) { $this->address = $address; } } class Business extends Client{ //The address attribute can be used here } // Class Individual class Individual extends Client{ //The address attribute can be used here }

But what if there is another class called order that needs to access the same address attribute? The order class cannot inherit the client class because this does not comply with the OOP principle. Traits come in handy at this time. You can define a trait to define these public properties.

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

// Trait Address

trait Address{

private $address;

public getAddress() {

eturn $this->address;

}

public setAddress($address) {

$this->address = $address;

}

}

// Class Business

class Business{

use Address;

// 这里可以使用address属性

}

// Class Individual

class Individual{

use Address;

//这里可以使用address属性

}

// Class Order

class Order{

use Address;

//这里可以使用address属性

}

1

2 3

45 6 7 8 9 10
11
12
13 14 15 16 17 18 19 20 21 22 23 24 25
// Trait Address trait Address{ private $address; public getAddress() { eturn $this->address; } public setAddress($address) { $this->address = $address; } } // Class Business class Business{ use Address; //The address attribute can be used here } // Class Individual class Individual{ use Address; //The address attribute can be used here } // Class Order class Order{ use Address; //The address attribute can be used here }
This is much more convenient! http://www.bkjia.com/PHPjc/1000096.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1000096.htmlTechArticle Simple usage examples of traits in PHP This article mainly introduces simple usage examples of traits in PHP. This article focuses on explaining The syntax of traits, what functions traits have, and when to use traits...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn