Hướng dẫn traits in php javatpoint - đặc điểm trong php javatpoint


PHP - Đặc điểm là gì?

PHP chỉ hỗ trợ thừa kế đơn: Một lớp con chỉ có thể thừa hưởng từ một cha mẹ đơn lẻ.

Vì vậy, nếu một lớp cần thừa hưởng nhiều hành vi? Đặc điểm OOP giải quyết vấn đề này.

Đặc điểm được sử dụng để khai báo các phương thức có thể được sử dụng trong nhiều lớp. Các đặc điểm có thể có các phương thức và phương thức trừu tượng có thể được sử dụng trong nhiều lớp và các phương thức có thể có bất kỳ công cụ sửa đổi truy cập nào (công khai, riêng tư hoặc được bảo vệ).

Đặc điểm được khai báo với từ khóa trait:

Cú pháp

trait TraitName {
  // some code...
}
?>

Để sử dụng một đặc điểm trong một lớp, hãy sử dụng từ khóa use:

Cú pháp

class MyClass {
  use TraitName;
}
?>

Để sử dụng một đặc điểm trong một lớp, hãy sử dụng từ khóa use:

Hãy xem xét một ví dụ:

Thí dụ
trait message1 {
public function msg1() {
    echo "OOP is fun! ";
  }
}

class Welcome {
  use message1;
}

lớp chào mừng {& nbsp; sử dụng message1;}
$obj->msg1();
?>

$ obj = new Welcome (); $ obj-> msg1 ();?>

Hãy tự mình thử »

Ví dụ giải thích

Ở đây, chúng tôi tuyên bố một đặc điểm: message1. Sau đó, chúng tôi tạo ra một lớp học: Chào mừng. Lớp học sử dụng đặc điểm và tất cả các phương thức trong tính trạng sẽ có sẵn trong lớp.



Nếu các lớp khác cần sử dụng hàm msg1 (), chỉ cần sử dụng đặc điểm Message1 trong các lớp đó. Điều này làm giảm sự trùng lặp mã, bởi vì không cần phải điều chỉnh lại phương thức tương tự nhiều lần.

PHP - Sử dụng nhiều đặc điểm

Hãy xem xét một ví dụ:

Thí dụ
trait message1 {
  public function msg1() {
    echo "OOP is fun! ";
  }
}

trait message2 {
  public function msg2() {
    echo "OOP reduces code duplication!";
  }
}

class Welcome {
  use message1;
}

lớp chào mừng {& nbsp; sử dụng message1;}
  use message1, message2;
}

$ obj = new Welcome (); $ obj-> msg1 ();?>
$obj->msg1();
echo "
";

Hãy tự mình thử »
$obj2->msg1();
$obj2->msg2();
?>

$ obj = new Welcome (); $ obj-> msg1 ();?>

Hãy tự mình thử »

Ví dụ giải thích



9 năm trước

Chris Dot Rutledge tại Gmail Dot Com ¶

Qeremy (!) Gmail ¶

7 năm trước

trait ezcReflectionReturnInfo {
    function 
getReturnType() { /*1*/ }
    function 
getReturnDescription() { /*2*/ }
}

class

ezcReflectionMethod extends ReflectionMethod {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}

class

ezcReflectionFunction extends ReflectionFunction {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}
?>

T8 tại At Pobox Dot Com ¶

RAWSRC ¶

4 năm trước

Canufrank ¶

class Base {
    public function 
sayHello() {
        echo 
'Hello ';
    }
}

trait

SayWorld {
    public function 
sayHello() {
        
parent::sayHello();
        echo 
'World!';
    }
}

class

MyHelloWorld extends Base {
    use 
SayWorld;
}
$o = new MyHelloWorld();
$o->sayHello();
?>

Ví dụ trên sẽ xuất ra:

Ví dụ #3 Ví dụ về thứ tự ưu tiên thay thế

trait HelloWorld {
    public function 
sayHello() {
        echo 
'Hello World!';
    }
}

class

use0

Ví dụ trên sẽ xuất ra:

Nhiều đặc điểm

Nhiều đặc điểm có thể được chèn vào một lớp bằng cách liệt kê chúng trong câu lệnh use1, được phân tách bằng dấu phẩy.

Ví dụ #4 Cách sử dụng nhiều đặc điểm

use2

trait

use4

Ví dụ trên sẽ xuất ra:

Giải quyết xung đột

Nếu hai đặc điểm chèn một phương thức có cùng tên, một lỗi gây tử vong được tạo ra, nếu xung đột không được giải quyết rõ ràng.

Để giải quyết xung đột đặt tên giữa các đặc điểm được sử dụng trong cùng một lớp, toán tử use5 cần được sử dụng để chọn chính xác một trong các phương pháp mâu thuẫn.

Vì điều này chỉ cho phép người ta loại trừ các phương thức, toán tử use6 có thể được sử dụng để thêm bí danh vào một trong các phương thức. Lưu ý toán tử use6 không đổi tên phương thức và nó cũng không ảnh hưởng đến bất kỳ phương thức nào khác.

Ví dụ #5 Giải quyết xung đột

Trong ví dụ này, người nói chuyện sử dụng các đặc điểm A và B. Vì A và B có các phương pháp mâu thuẫn, nó xác định để sử dụng biến thể của SmallTalk từ Đặc điểm B và biến thể của BigTalk từ Đặc điểm A.

Bí danh_Talker sử dụng toán tử use6 để có thể sử dụng triển khai BigTalk của B theo bí danh bổ sung use9.

trait ezcReflectionReturnInfo {
    function 
getReturnType() { /*1*/ }
    function 
getReturnDescription() { /*2*/ }
}
0

trait

trait ezcReflectionReturnInfo {
    function 
getReturnType() { /*1*/ }
    function 
getReturnDescription() { /*2*/ }
}
2

Thay đổi khả năng hiển thị phương pháp

Sử dụng cú pháp use6, người ta cũng có thể điều chỉnh khả năng hiển thị của phương pháp trong lớp triển lãm.

Ví dụ #6 Thay đổi khả năng hiển thị phương pháp

trait ezcReflectionReturnInfo {
    function 
getReturnType() { /*1*/ }
    function 
getReturnDescription() { /*2*/ }
}
4

Những đặc điểm sáng tác từ các đặc điểm

Giống như các lớp có thể sử dụng các đặc điểm, các đặc điểm khác cũng vậy. Bằng cách sử dụng một hoặc nhiều đặc điểm trong một định nghĩa đặc điểm, nó có thể được cấu tạo một phần hoặc hoàn toàn các thành viên được xác định trong các đặc điểm khác.

Ví dụ #7 Đặc điểm sáng tác từ các đặc điểm

use2

trait

trait ezcReflectionReturnInfo {
    function 
getReturnType() { /*1*/ }
    function 
getReturnDescription() { /*2*/ }
}
7

Ví dụ trên sẽ xuất ra:

Các thành viên tính trạng trừu tượng

Các đặc điểm hỗ trợ việc sử dụng các phương pháp trừu tượng để áp đặt các yêu cầu đối với lớp triển lãm. Các phương pháp công khai, được bảo vệ và riêng tư được hỗ trợ. Trước PHP 8.0.0, chỉ các phương pháp trừu tượng được công khai và được bảo vệ được hỗ trợ.

Thận trọng

Một lớp cụ thể đáp ứng yêu cầu này bằng cách xác định phương pháp cụ thể có cùng tên; Chữ ký của nó có thể khác nhau.

Ví dụ #8 Yêu cầu rõ ràng bằng các phương thức trừu tượng

trait ezcReflectionReturnInfo {
    function 
getReturnType() { /*1*/ }
    function 
getReturnDescription() { /*2*/ }
}
8

class

class0

Thành viên đặc điểm tĩnh

Đặc điểm có thể xác định các biến tĩnh, phương thức tĩnh và tính chất tĩnh.

Ghi chú::

Kể từ Php 8.1.0, việc gọi phương thức tĩnh hoặc truy cập vào một thuộc tính tĩnh trực tiếp trên một đặc điểm không được chấp nhận. Các phương thức và thuộc tính tĩnh chỉ nên được truy cập trên một lớp bằng cách sử dụng đặc điểm.

Ví dụ #9 biến tĩnh

class1

class

class3

Ví dụ #10 Phương pháp tĩnh

class4

class

class6

Ví dụ #11 thuộc tính tĩnh

class7

class

class9

Đặc tính

Đặc điểm cũng có thể xác định các thuộc tính.

Ví dụ #12 Thuộc tính xác định

ezcReflectionMethod extends ReflectionMethod {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}

class

ezcReflectionFunction extends ReflectionFunction {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}
?>
0

class

ezcReflectionMethod extends ReflectionMethod {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}

class

ezcReflectionFunction extends ReflectionFunction {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}
?>
2

Nếu một đặc điểm xác định một thuộc tính thì một lớp không thể xác định thuộc tính có cùng tên trừ khi nó tương thích (cùng một tầm nhìn và giá trị ban đầu), nếu không một lỗi gây tử vong được phát hành.

Ví dụ #13 Giải quyết xung đột

ezcReflectionMethod extends ReflectionMethod {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}

class

ezcReflectionFunction extends ReflectionFunction {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}
?>
3

class

ezcReflectionMethod extends ReflectionMethod {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}

class

ezcReflectionFunction extends ReflectionFunction {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}
?>
5

Safak Ozpinar / Safakozpinar tại Gmail ¶

10 năm trước

ezcReflectionMethod extends ReflectionMethod {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}

class

ezcReflectionFunction extends ReflectionFunction {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}
?>
6

ezcReflectionMethod extends ReflectionMethod {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}

class

ezcReflectionFunction extends ReflectionFunction {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}
?>
7

ezcReflectionMethod extends ReflectionMethod {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}

class

ezcReflectionFunction extends ReflectionFunction {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}
?>
8

Greywire tại Gmail Dot Com ¶

10 năm trước

ezcReflectionMethod extends ReflectionMethod {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}

class

ezcReflectionFunction extends ReflectionFunction {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}
?>
9

class Base {
    public function 
sayHello() {
        echo 
'Hello ';
    }
}
0

ezcReflectionMethod extends ReflectionMethod {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}

class

ezcReflectionFunction extends ReflectionFunction {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}
?>
8

Greywire tại Gmail Dot Com ¶

Stefan W ¶

class Base {
    public function 
sayHello() {
        echo 
'Hello ';
    }
}
2

class Base {
    public function 
sayHello() {
        echo 
'Hello ';
    }
}
3

class Base {
    public function 
sayHello() {
        echo 
'Hello ';
    }
}
4

ezcReflectionMethod extends ReflectionMethod {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}

class

ezcReflectionFunction extends ReflectionFunction {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}
?>
8

9 năm trước

10 năm trước

class Base {
    public function 
sayHello() {
        echo 
'Hello ';
    }
}
6

class Base {
    public function 
sayHello() {
        echo 
'Hello ';
    }
}
7

class Base {
    public function 
sayHello() {
        echo 
'Hello ';
    }
}
8

class Base {
    public function 
sayHello() {
        echo 
'Hello ';
    }
}
9

trait0

Greywire tại Gmail Dot Com ¶

Stefan W ¶

trait1

trait2

trait3

trait4

9 năm trước

10 năm trước

trait5

trait6

trait7

class Base {
    public function 
sayHello() {
        echo 
'Hello ';
    }
}
9

trait9

Greywire tại Gmail Dot Com ¶

Stefan W ¶

SayWorld {
    public function 
sayHello() {
        
parent::sayHello();
        echo 
'World!';
    }
}

class

MyHelloWorld extends Base {
    use 
SayWorld;
}
$o = new MyHelloWorld();
$o->sayHello();
?>
0

class Base {
    public function 
sayHello() {
        echo 
'Hello ';
    }
}
9

SayWorld {
    public function 
sayHello() {
        
parent::sayHello();
        echo 
'World!';
    }
}

class

MyHelloWorld extends Base {
    use 
SayWorld;
}
$o = new MyHelloWorld();
$o->sayHello();
?>
2

9 năm trước

Chris Dot Rutledge tại Gmail Dot Com ¶

SayWorld {
    public function 
sayHello() {
        
parent::sayHello();
        echo 
'World!';
    }
}

class

MyHelloWorld extends Base {
    use 
SayWorld;
}
$o = new MyHelloWorld();
$o->sayHello();
?>
3

SayWorld {
    public function 
sayHello() {
        
parent::sayHello();
        echo 
'World!';
    }
}

class

MyHelloWorld extends Base {
    use 
SayWorld;
}
$o = new MyHelloWorld();
$o->sayHello();
?>
4

SayWorld {
    public function 
sayHello() {
        
parent::sayHello();
        echo 
'World!';
    }
}

class

MyHelloWorld extends Base {
    use 
SayWorld;
}
$o = new MyHelloWorld();
$o->sayHello();
?>
5

SayWorld {
    public function 
sayHello() {
        
parent::sayHello();
        echo 
'World!';
    }
}

class

MyHelloWorld extends Base {
    use 
SayWorld;
}
$o = new MyHelloWorld();
$o->sayHello();
?>
6

ezcReflectionMethod extends ReflectionMethod {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}

class

ezcReflectionFunction extends ReflectionFunction {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}
?>
8

Qeremy (!) Gmail ¶

Chris Dot Rutledge tại Gmail Dot Com ¶

SayWorld {
    public function 
sayHello() {
        
parent::sayHello();
        echo 
'World!';
    }
}

class

MyHelloWorld extends Base {
    use 
SayWorld;
}
$o = new MyHelloWorld();
$o->sayHello();
?>
8

SayWorld {
    public function 
sayHello() {
        
parent::sayHello();
        echo 
'World!';
    }
}

class

MyHelloWorld extends Base {
    use 
SayWorld;
}
$o = new MyHelloWorld();
$o->sayHello();
?>
9

ezcReflectionMethod extends ReflectionMethod {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}

class

ezcReflectionFunction extends ReflectionFunction {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}
?>
8

Qeremy (!) Gmail ¶

Chris Dot Rutledge tại Gmail Dot Com ¶

trait HelloWorld {
    public function 
sayHello() {
        echo 
'Hello World!';
    }
}
1

trait HelloWorld {
    public function 
sayHello() {
        echo 
'Hello World!';
    }
}
2

trait HelloWorld {
    public function 
sayHello() {
        echo 
'Hello World!';
    }
}
3

class Base {
    public function 
sayHello() {
        echo 
'Hello ';
    }
}
9

trait HelloWorld {
    public function 
sayHello() {
        echo 
'Hello World!';
    }
}
5

Qeremy (!) Gmail ¶

7 năm trước

trait HelloWorld {
    public function 
sayHello() {
        echo 
'Hello World!';
    }
}
6

trait HelloWorld {
    public function 
sayHello() {
        echo 
'Hello World!';
    }
}
7

trait HelloWorld {
    public function 
sayHello() {
        echo 
'Hello World!';
    }
}
8

trait HelloWorld {
    public function 
sayHello() {
        echo 
'Hello World!';
    }
}
9

T8 tại At Pobox Dot Com ¶

10 năm trước

class0

class1

ezcReflectionMethod extends ReflectionMethod {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}

class

ezcReflectionFunction extends ReflectionFunction {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}
?>
8

Greywire tại Gmail Dot Com ¶

10 năm trước

class3

class4

class5

class6

Greywire tại Gmail Dot Com ¶

7 năm trước

class7

class8

ezcReflectionMethod extends ReflectionMethod {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}

class

ezcReflectionFunction extends ReflectionFunction {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}
?>
8

T8 tại At Pobox Dot Com ¶

RAWSRC ¶

use00

use01

class Base {
    public function 
sayHello() {
        echo 
'Hello ';
    }
}
9

use03

4 năm trước

Stefan W ¶

use04

use05

use06

use07

use08

9 năm trước

Stefan W ¶

use09

use10

use11

class Base {
    public function 
sayHello() {
        echo 
'Hello ';
    }
}
9

use13

9 năm trước

Chris Dot Rutledge tại Gmail Dot Com ¶

use14

use15

use16

use17

use18

Qeremy (!) Gmail ¶

7 năm trước

use19

use20

use21

use22

T8 tại At Pobox Dot Com ¶

6 năm trước

use23

use24

use25

use26

use27

use28

use29

use26

use31

ezcReflectionMethod extends ReflectionMethod {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}

class

ezcReflectionFunction extends ReflectionFunction {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}
?>
8

Kỳ lạ ¶

9 năm trước

use33

Bscheshirwork tại Gmail Dot Com ¶

4 năm trước

use34

use35

use36

use37

use38

use39

use40

use41

use39

use43

use44

use45

use46

use47

use48

use49

use50

use51

use52

ezcReflectionMethod extends ReflectionMethod {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}

class

ezcReflectionFunction extends ReflectionFunction {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}
?>
8

84TD84 tại gmail dot com ¶

7 năm trước

use54

use55

use56

use57

use58

use59

use60

ezcReflectionMethod extends ReflectionMethod {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}

class

ezcReflectionFunction extends ReflectionFunction {
    use 
ezcReflectionReturnInfo;
    
/* ... */
}
?>
8