Đó là một khái niệm truy cập các tính năng của một lớp từ một lớp khác. Nếu chúng ta kế thừa các tính năng của lớp vào một lớp khác, chúng ta có thể truy cập cả hai thuộc tính của lớp. Chúng ta có thể mở rộng các tính năng của một lớp bằng cách sử dụng từ khóa 'extends'
- Nó hỗ trợ khái niệm phân loại theo thứ bậc
- Kế thừa có ba loại, Kế thừa đơn, nhiều và đa cấp
- PHP chỉ hỗ trợ kế thừa đơn, trong đó chỉ có một lớp có thể được dẫn xuất từ lớp cha đơn
- Chúng ta có thể mô phỏng đa kế thừa bằng cách sử dụng các giao diện
ví dụ 1
đầu ra
ví dụ 2
đầu ra
Kế thừa là một nguyên tắc quan trọng của phương pháp lập trình hướng đối tượng. Sử dụng nguyên tắc này, mối quan hệ giữa hai lớp có thể được định nghĩa. PHP hỗ trợ kế thừa trong mô hình đối tượng của nó
PHP sử dụng từ khóa extends để thiết lập quan hệ giữa hai lớp
cú pháp
class B extends A
trong đó A là lớp cơ sở [còn được gọi là lớp cha] và B được gọi là lớp con hoặc lớp con. Lớp con kế thừa các phương thức public và protected của lớp cha. Lớp con có thể xác định lại hoặc ghi đè bất kỳ phương thức kế thừa nào. Nếu không, các phương thức được kế thừa sẽ giữ nguyên chức năng của chúng như được định nghĩa trong lớp cha, khi được sử dụng với đối tượng của lớp con
Định nghĩa lớp cha phải đi trước định nghĩa lớp con. Trong trường hợp này, định nghĩa của lớp A sẽ xuất hiện trước định nghĩa của lớp B trong tập lệnh
Thí dụ
Nếu tính năng tự động tải được bật, định nghĩa của lớp cha sẽ được lấy bằng cách tải tập lệnh lớp
Kế thừa ví dụ
Đoạn mã sau cho thấy rằng lớp con kế thừa các thành viên public và protected của lớp cha
Thí dụ
Bản thử trực tiếp
" ; } protected function protectedmethod[]{ echo "This is protected method of parent class
" ; } private function privatemethod[]{ echo "This is private method of parent class
" ; } } class childclass extends parentclass{ public function childmethod[]{ $this->protectedmethod[]; //$this->privatemethod[]; //this will produce error } } $obj=new childclass[]; $obj->publicmethod[]; $obj->childmethod[]; ?>
đầu ra
Điều này sẽ tạo ra kết quả sau. -
This is public method of parent class This is protected method of parent class PHP Fatal error: Uncaught Error: Call to private method parentclass::privatemethod[] from context 'childclass'
Ví dụ ghi đè phương thức
Nếu một phương thức kế thừa từ lớp cha được định nghĩa lại trong lớp con, định nghĩa mới sẽ ghi đè chức năng trước đó. Trong ví dụ sau, publicmethod được định nghĩa lại trong lớp con
Thí dụ
Bản thử trực tiếp
" ; } protected function protectedmethod[]{ echo "This is protected method of parent class
" ; } private function privatemethod[]{ echo "This is private method of parent class
" ; } } class childclass extends parentclass{ public function publicmethod[]{ echo "public method of parent class is overridden in child class
" ; } } $obj=new childclass[]; $obj->publicmethod[]; ?>
đầu ra
Điều này sẽ tạo ra kết quả sau. -
public method of parent class is overridden in child class
Kế thừa phân cấp
PHP không hỗ trợ đa kế thừa. Do đó một lớp không thể mở rộng hai hoặc nhiều lớp. Tuy nhiên, nó hỗ trợ kế thừa phân cấp như sau
Trong lập trình hướng đối tượng, Kế thừa cho phép một lớp sử dụng các thuộc tính và phương thức của một lớp hiện có. Thông thường, trong khi mã hóa, chúng ta gặp các tình huống trong đó chúng ta phải tạo một lớp mới với tất cả các chức năng của một lớp hiện có và một số phương thức bổ sung, giống như một phần mở rộng cho một lớp hiện có, trong những trường hợp như vậy, chúng ta có thể sao chép tất cả các thuộc tính và
Bối rối? . Hãy xem xét một lớp
" ; } protected function protectedmethod[]{ echo "This is protected method of parent class3 với các phương thức cơ bản trong đó như
" ; } private function privatemethod[]{ echo "This is private method of parent class
" ; } } class childclass extends parentclass{ public function publicmethod[]{ echo "public method of parent class is overridden in child class
" ; } } $obj=new childclass[]; $obj->publicmethod[]; ?>
" ; } protected function protectedmethod[]{ echo "This is protected method of parent class4,
" ; } private function privatemethod[]{ echo "This is private method of parent class
" ; } } class childclass extends parentclass{ public function publicmethod[]{ echo "public method of parent class is overridden in child class
" ; } } $obj=new childclass[]; $obj->publicmethod[]; ?>
" ; } protected function protectedmethod[]{ echo "This is protected method of parent class5,
" ; } private function privatemethod[]{ echo "This is private method of parent class
" ; } } class childclass extends parentclass{ public function publicmethod[]{ echo "public method of parent class is overridden in child class
" ; } } $obj=new childclass[]; $obj->publicmethod[]; ?>
" ; } protected function protectedmethod[]{ echo "This is protected method of parent class6,
" ; } private function privatemethod[]{ echo "This is private method of parent class
" ; } } class childclass extends parentclass{ public function publicmethod[]{ echo "public method of parent class is overridden in child class
" ; } } $obj=new childclass[]; $obj->publicmethod[]; ?>
name. " is walking...
";
}
// public function see
public function see[] {
echo $this->name. " is seeing...
";
}
}
// child class
class Male extends Human {
// No code in child
}
// child class
class Female extends Human {
// No code in child
}
$male = new Male[];
$male->name = "Adam";
$female = new Female[];
$female->name = "Eve";
// calling Human class methods
// check the output [hehe, pun intended]
$female->walk[];
$male->see[];
?>
0, v.v. Bây giờ nếu chúng ta phải tạo thêm hai lớp cho nam và nữ với tên name. " is walking...
";
}
// public function see
public function see[] {
echo $this->name. " is seeing...
";
}
}
// child class
class Male extends Human {
// No code in child
}
// child class
class Female extends Human {
// No code in child
}
$male = new Male[];
$male->name = "Adam";
$female = new Female[];
$female->name = "Eve";
// calling Human class methods
// check the output [hehe, pun intended]
$female->walk[];
$male->see[];
?>
1 và name. " is walking...
";
}
// public function see
public function see[] {
echo $this->name. " is seeing...
";
}
}
// child class
class Male extends Human {
// No code in child
}
// child class
class Female extends Human {
// No code in child
}
$male = new Male[];
$male->name = "Adam";
$female = new Female[];
$female->name = "Eve";
// calling Human class methods
// check the output [hehe, pun intended]
$female->walk[];
$male->see[];
?>
2, với tất cả các thuộc tính và phương thức của lớp " ; } protected function protectedmethod[]{ echo "This is protected method of parent class3 và một số tính năng cụ thể chỉ có sẵn cho
" ; } private function privatemethod[]{ echo "This is private method of parent class
" ; } } class childclass extends parentclass{ public function publicmethod[]{ echo "public method of parent class is overridden in child class
" ; } } $obj=new childclass[]; $obj->publicmethod[]; ?>
name. " is walking...
";
}
// public function see
public function see[] {
echo $this->name. " is seeing...
";
}
}
// child class
class Male extends Human {
// No code in child
}
// child class
class Female extends Human {
// No code in child
}
$male = new Male[];
$male->name = "Adam";
$female = new Female[];
$female->name = "Eve";
// calling Human class methods
// check the output [hehe, pun intended]
$female->walk[];
$male->see[];
?>
1 và name. " is walking...
";
}
// public function see
public function see[] {
echo $this->name. " is seeing...
";
}
}
// child class
class Male extends Human {
// No code in child
}
// child class
class Female extends Human {
// No code in child
}
$male = new Male[];
$male->name = "Adam";
$female = new Female[];
$female->name = "Eve";
// calling Human class methods
// check the output [hehe, pun intended]
$female->walk[];
$male->see[];
?>
2, chúng ta có thể làm như vậy bằng cách kế thừa lớp " ; } protected function protectedmethod[]{ echo "This is protected method of parent class3 trong
" ; } private function privatemethod[]{ echo "This is private method of parent class
" ; } } class childclass extends parentclass{ public function publicmethod[]{ echo "public method of parent class is overridden in child class
" ; } } $obj=new childclass[]; $obj->publicmethod[]; ?>
name. " is walking...
";
}
// public function see
public function see[] {
echo $this->name. " is seeing...
";
}
}
// child class
class Male extends Human {
// No code in child
}
// child class
class Female extends Human {
// No code in child
}
$male = new Male[];
$male->name = "Adam";
$female = new Female[];
$female->name = "Eve";
// calling Human class methods
// check the output [hehe, pun intended]
$female->walk[];
$male->see[];
?>
1 Lớp được kế thừa được gọi là Lớp cha [hoặc lớp siêu cấp hoặc lớp cơ sở] trong khi lớp đang kế thừa lớp khác được gọi là Lớp con [hoặc lớp con hoặc lớp dẫn xuất]
Trong ví dụ trên,
" ; } protected function protectedmethod[]{ echo "This is protected method of parent class3 sẽ là lớp cha và
" ; } private function privatemethod[]{ echo "This is private method of parent class
" ; } } class childclass extends parentclass{ public function publicmethod[]{ echo "public method of parent class is overridden in child class
" ; } } $obj=new childclass[]; $obj->publicmethod[]; ?>
name. " is walking...
";
}
// public function see
public function see[] {
echo $this->name. " is seeing...
";
}
}
// child class
class Male extends Human {
// No code in child
}
// child class
class Female extends Human {
// No code in child
}
$male = new Male[];
$male->name = "Adam";
$female = new Female[];
$female->name = "Eve";
// calling Human class methods
// check the output [hehe, pun intended]
$female->walk[];
$male->see[];
?>
1 và name. " is walking...
";
}
// public function see
public function see[] {
echo $this->name. " is seeing...
";
}
}
// child class
class Male extends Human {
// No code in child
}
// child class
class Female extends Human {
// No code in child
}
$male = new Male[];
$male->name = "Adam";
$female = new Female[];
$female->name = "Eve";
// calling Human class methods
// check the output [hehe, pun intended]
$female->walk[];
$male->see[];
?>
2 sẽ là lớp con của nóMột lớp có thể được kế thừa bởi nhiều lớp
Kế thừa rất hữu ích nếu chúng ta muốn tạo một số lớp tương tự. Chúng ta có thể đặt các thuộc tính và phương thức chung trong một lớp cha và sau đó kế thừa nó trong các lớp con
Và để hủy đối tượng, phương thức Destructor được sử dụng
Cú pháp kế thừa một lớp
Trong PHP, từ khóa
" ; } protected function protectedmethod[]{ echo "This is protected method of parent class42 được sử dụng để chỉ định tên của lớp cha trong khi định nghĩa lớp con. Ví dụ,
" ; } private function privatemethod[]{ echo "This is private method of parent class
" ; } } class childclass extends parentclass{ public function childmethod[]{ $this->protectedmethod[]; //$this->privatemethod[]; //this will produce error } } $obj=new childclass[]; $obj->publicmethod[]; $obj->childmethod[]; ?>
" ; } protected function protectedmethod[]{ echo "This is protected method of parent class5
" ; } private function privatemethod[]{ echo "This is private method of parent class
" ; } } class childclass extends parentclass{ public function childmethod[]{ $this->protectedmethod[]; //$this->privatemethod[]; //this will produce error } } $obj=new childclass[]; $obj->publicmethod[]; $obj->childmethod[]; ?>
Một số điểm quan trọng cần nhớ khi sử dụng thừa kế là
- Lớp con chỉ có thể truy cập và sử dụng các thuộc tính và phương thức không riêng tư trên lớp cha
- Lớp con cũng có thể có các phương thức riêng, lớp cha sẽ không có phương thức này
- Lớp con cũng có thể ghi đè một phương thức được định nghĩa trong lớp cha và cung cấp triển khai riêng cho nó
Hãy thêm một vài phương thức vào lớp
" ; } protected function protectedmethod[]{ echo "This is protected method of parent class3 của chúng ta và xem cách chúng ta có thể sử dụng chúng trong các lớp con
" ; } private function privatemethod[]{ echo "This is private method of parent class
" ; } } class childclass extends parentclass{ public function publicmethod[]{ echo "public method of parent class is overridden in child class
" ; } } $obj=new childclass[]; $obj->publicmethod[]; ?>
name. " is walking...
";
}
// public function see
public function see[] {
echo $this->name. " is seeing...
";
}
}
// child class
class Male extends Human {
// No code in child
}
// child class
class Female extends Human {
// No code in child
}
$male = new Male[];
$male->name = "Adam";
$female = new Female[];
$female->name = "Eve";
// calling Human class methods
// check the output [hehe, pun intended]
$female->walk[];
$male->see[];
?>
1 và name. " is walking...
";
}
// public function see
public function see[] {
echo $this->name. " is seeing...
";
}
}
// child class
class Male extends Human {
// No code in child
}
// child class
class Female extends Human {
// No code in child
}
$male = new Male[];
$male->name = "Adam";
$female = new Female[];
$female->name = "Eve";
// calling Human class methods
// check the output [hehe, pun intended]
$female->walk[];
$male->see[];
?>
2name. " is walking...
";
}
// public function see
public function see[] {
echo $this->name. " is seeing...
";
}
}
// child class
class Male extends Human {
// No code in child
}
// child class
class Female extends Human {
// No code in child
}
$male = new Male[];
$male->name = "Adam";
$female = new Female[];
$female->name = "Eve";
// calling Human class methods
// check the output [hehe, pun intended]
$female->walk[];
$male->see[];
?>
Đêm giao thừa đang đi bộ. Adam đang nhìn thấy
Như bạn có thể thấy từ đoạn mã trên, cả hai lớp con đều trống, chúng tôi chỉ kế thừa lớp
" ; } protected function protectedmethod[]{ echo "This is protected method of parent class3 trong cả hai lớp, cho phép chúng truy cập và sử dụng các thuộc tính và phương thức thành viên trên lớp cha
" ; } private function privatemethod[]{ echo "This is private method of parent class
" ; } } class childclass extends parentclass{ public function publicmethod[]{ echo "public method of parent class is overridden in child class
" ; } } $obj=new childclass[]; $obj->publicmethod[]; ?>
Lớp con với các phương thức và thuộc tính riêng của nó
Khi một lớp con kế thừa một lớp cha, nó có thể truy cập và sử dụng tất cả các thành viên không riêng tư của lớp cha. Chúng tôi biết điều đó, nhưng một lớp con có thể có các thuộc tính và phương thức thành viên riêng không? . Hãy lấy một ví dụ khác để xem làm thế nào chúng ta có thể làm điều này
" ; } protected function protectedmethod[]{ echo "This is protected method of parent class4
" ; } private function privatemethod[]{ echo "This is private method of parent class
" ; } } class childclass extends parentclass{ public function childmethod[]{ $this->protectedmethod[]; //$this->privatemethod[]; //this will produce error } } $obj=new childclass[]; $obj->publicmethod[]; $obj->childmethod[]; ?>
Mercedes benz - Khởi động động cơ. Tôi là Mercedes benz Hãy tiếp tục lái xe
Khi một lớp kế thừa một lớp khác, nó có lợi thế là có thể sử dụng các thuộc tính và phương thức được định nghĩa trong lớp cha mà không cần định nghĩa lại chúng. Và cũng có thể có các thuộc tính và phương thức riêng giống như bất kỳ lớp bình thường nào khác
Công cụ sửa đổi truy cập " ;
}
protected function protectedmethod[]{
echo "This is protected method of parent class
" ;
}
private function privatemethod[]{
echo "This is private method of parent class
" ;
}
}
class childclass extends parentclass{
public function childmethod[]{
$this->protectedmethod[];
//$this->privatemethod[]; //this will produce error
}
}
$obj=new childclass[];
$obj->publicmethod[];
$obj->childmethod[];
?>
47
" ; } private function privatemethod[]{ echo "This is private method of parent class
" ; } } class childclass extends parentclass{ public function childmethod[]{ $this->protectedmethod[]; //$this->privatemethod[]; //this will produce error } } $obj=new childclass[]; $obj->publicmethod[]; $obj->childmethod[]; ?>
Chúng tôi đã tìm hiểu về các công cụ sửa đổi truy cập khác nhau và cách chúng tôi có thể sử dụng chúng để kiểm soát quyền truy cập đối với các thuộc tính và phương thức khác nhau của một lớp
Khi một lớp con kế thừa một lớp cha, nó chỉ có thể truy cập và sử dụng lại các thuộc tính và phương thức không riêng tư. Nhưng chúng ta không nên sử dụng công cụ sửa đổi truy cập
" ; } protected function protectedmethod[]{ echo "This is protected method of parent class48 cho các thuộc tính, vì khi đó các thuộc tính cũng có thể được truy cập từ bên ngoài lớp
" ; } private function privatemethod[]{ echo "This is private method of parent class
" ; } } class childclass extends parentclass{ public function childmethod[]{ $this->protectedmethod[]; //$this->privatemethod[]; //this will produce error } } $obj=new childclass[]; $obj->publicmethod[]; $obj->childmethod[]; ?>
Để chỉ cho phép lớp con truy cập các thuộc tính và phương thức của lớp cha, chúng ta có thể sử dụng công cụ sửa đổi truy cập
" ; } protected function protectedmethod[]{ echo "This is protected method of parent class47
" ; } private function privatemethod[]{ echo "This is private method of parent class
" ; } } class childclass extends parentclass{ public function childmethod[]{ $this->protectedmethod[]; //$this->privatemethod[]; //this will produce error } } $obj=new childclass[]; $obj->publicmethod[]; $obj->childmethod[]; ?>
Khi chúng ta định nghĩa bất kỳ thuộc tính hoặc phương thức nào của một lớp là
" ; } protected function protectedmethod[]{ echo "This is protected method of parent class47 thì những thuộc tính và phương thức đó chỉ có thể được truy cập trong lớp con kế thừa lớp đó
" ; } private function privatemethod[]{ echo "This is private method of parent class
" ; } } class childclass extends parentclass{ public function childmethod[]{ $this->protectedmethod[]; //$this->privatemethod[]; //this will produce error } } $obj=new childclass[]; $obj->publicmethod[]; $obj->childmethod[]; ?>
Hãy lấy một ví dụ
This is public method of parent class This is protected method of parent class PHP Fatal error: Uncaught Error: Call to private method parentclass::privatemethod[] from context 'childclass'4
Mercedes benz - Khởi động động cơ. Tôi là Mercedes benz Hãy tiếp tục lái xe
Trong ví dụ trên, chúng tôi đã tạo biến
This is public method of parent class This is protected method of parent class PHP Fatal error: Uncaught Error: Call to private method parentclass::privatemethod[] from context 'childclass'41 là
" ; } protected function protectedmethod[]{ echo "This is protected method of parent class47, hãy thử chạy cùng mã với tên là
" ; } private function privatemethod[]{ echo "This is private method of parent class
" ; } } class childclass extends parentclass{ public function childmethod[]{ $this->protectedmethod[]; //$this->privatemethod[]; //this will produce error } } $obj=new childclass[]; $obj->publicmethod[]; $obj->childmethod[]; ?>
This is public method of parent class This is protected method of parent class PHP Fatal error: Uncaught Error: Call to private method parentclass::privatemethod[] from context 'childclass'43, bạn sẽ gặp lỗi sau
Để ý. Tài sản không xác định
Ghi đè phương thức của lớp cha
Điều gì sẽ xảy ra nếu một lớp con muốn sử dụng một phương thức của lớp cha nhưng hơi khác một chút? . Điều này được gọi là ghi đè phương thức
Hãy lấy một ví dụ để hiểu khái niệm này
This is public method of parent class This is protected method of parent class PHP Fatal error: Uncaught Error: Call to private method parentclass::privatemethod[] from context 'childclass'8
Phương pháp lái xe hạng ô tô. Phương pháp lái xe mô tô
Trong đoạn mã trên, chúng ta có một lớp cha tên là
This is public method of parent class This is protected method of parent class PHP Fatal error: Uncaught Error: Call to private method parentclass::privatemethod[] from context 'childclass'44 và hai lớp con mở rộng lớp cha là
This is public method of parent class This is protected method of parent class PHP Fatal error: Uncaught Error: Call to private method parentclass::privatemethod[] from context 'childclass'45 và
This is public method of parent class This is protected method of parent class PHP Fatal error: Uncaught Error: Call to private method parentclass::privatemethod[] from context 'childclass'46. Trong lớp cha, chúng ta có một phương thức ________ 247 mà chúng ta đã ghi đè trong các lớp con và đã cung cấp định nghĩa khác cho nó