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;
/* ... */
}
?>
2Nế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
classezcReflectionMethod extends ReflectionMethod {
3
use ezcReflectionReturnInfo;
/* ... */
}
use ezcReflectionReturnInfo;
/* ... */
}
?>
class
ezcReflectionMethod extends ReflectionMethod {
use ezcReflectionReturnInfo;
/* ... */
}class
ezcReflectionFunction extends ReflectionFunction {
use ezcReflectionReturnInfo;
/* ... */
}
?>
5Safak Ozpinar / Safakozpinar tại Gmail ¶ ¶
10 năm trước
classezcReflectionMethod extends ReflectionMethod {
6
use ezcReflectionReturnInfo;
/* ... */
}
use ezcReflectionReturnInfo;
/* ... */
}
?>
classezcReflectionMethod extends ReflectionMethod {
7
use ezcReflectionReturnInfo;
/* ... */
}
use ezcReflectionReturnInfo;
/* ... */
}
?>
ezcReflectionMethod extends ReflectionMethod {
use ezcReflectionReturnInfo;
/* ... */
}class
ezcReflectionFunction extends ReflectionFunction {
use ezcReflectionReturnInfo;
/* ... */
}
?>
8Greywire tại Gmail Dot Com ¶ ¶
10 năm trước
classezcReflectionMethod extends ReflectionMethod {
9
use ezcReflectionReturnInfo;
/* ... */
}
use ezcReflectionReturnInfo;
/* ... */
}
?>
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 ¶
classSayWorld {
3
public function sayHello[] {
parent::sayHello[];
echo 'World!';
}
}
use SayWorld;
}$o = new MyHelloWorld[];
$o->sayHello[];
?>
classSayWorld {
4
public function sayHello[] {
parent::sayHello[];
echo 'World!';
}
}
use SayWorld;
}$o = new MyHelloWorld[];
$o->sayHello[];
?>
classSayWorld {
5
public function sayHello[] {
parent::sayHello[];
echo 'World!';
}
}
use SayWorld;
}$o = new MyHelloWorld[];
$o->sayHello[];
?>
classSayWorld {
6
public function sayHello[] {
parent::sayHello[];
echo 'World!';
}
}
use SayWorld;
}$o = new MyHelloWorld[];
$o->sayHello[];
?>
ezcReflectionMethod extends ReflectionMethod {
use ezcReflectionReturnInfo;
/* ... */
}class
ezcReflectionFunction extends ReflectionFunction {
use ezcReflectionReturnInfo;
/* ... */
}
?>
8Qeremy [!] Gmail ¶ ¶
Chris Dot Rutledge tại Gmail Dot Com ¶
classSayWorld {
8
public function sayHello[] {
parent::sayHello[];
echo 'World!';
}
}
use SayWorld;
}$o = new MyHelloWorld[];
$o->sayHello[];
?>
classSayWorld {
9
public function sayHello[] {
parent::sayHello[];
echo 'World!';
}
}
use SayWorld;
}$o = new MyHelloWorld[];
$o->sayHello[];
?>
ezcReflectionMethod extends ReflectionMethod {
use ezcReflectionReturnInfo;
/* ... */
}class
ezcReflectionFunction extends ReflectionFunction {
use ezcReflectionReturnInfo;
/* ... */
}
?>
8Qeremy [!] Gmail ¶ ¶
Chris Dot Rutledge tại Gmail Dot Com ¶
8
Greywire tại Gmail Dot Com ¶ ¶
10 năm trước
class
3
class
4
class
5
class
6Greywire tại Gmail Dot Com ¶ ¶
7 năm trước
class
7
class
8
ezcReflectionMethod extends ReflectionMethod {
use ezcReflectionReturnInfo;
/* ... */
}class
ezcReflectionFunction extends ReflectionFunction {
use ezcReflectionReturnInfo;
/* ... */
}
?>
8T8 tại At Pobox Dot Com ¶ ¶
RAWSRC ¶
use
00
use
01
8
Kỳ lạ ¶ ¶
9 năm trước
use
33
Bscheshirwork tại Gmail Dot Com ¶ ¶
4 năm trước
use
34
use
35
use
36
use
37
use
38
use
39
use
40
use
41
use
39
use
43
use
44
use
45
use
46
use
47
use
48
use
49
use
50
use
51
use
52
ezcReflectionMethod extends ReflectionMethod {
use ezcReflectionReturnInfo;
/* ... */
}class
ezcReflectionFunction extends ReflectionFunction {
use ezcReflectionReturnInfo;
/* ... */
}
?>
884TD84 tại gmail dot com ¶ ¶
7 năm trước
use
54
use
55
use
56
use
57
use
58
use
59
use
60
ezcReflectionMethod extends ReflectionMethod {
use ezcReflectionReturnInfo;
/* ... */
}class
ezcReflectionFunction extends ReflectionFunction {
use ezcReflectionReturnInfo;
/* ... */
}
?>
8