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

Để 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

Để 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

T8 tại At Pobox Dot Com ¶

RAWSRC ¶

4 năm trước

Canufrank ¶

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

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

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

8

Greywire tại Gmail Dot Com ¶

Stefan W ¶

8

9 năm trước

10 năm trước

0

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 ¶

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

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

Bài Viết Liên Quan

Chủ Đề