Hướng dẫn can you override methods in php? - bạn có thể ghi đè các phương thức trong php không?

Tóm tắt: Trong hướng dẫn này, bạn sẽ tìm hiểu về phương pháp ghi đè PHP và cách áp dụng nó một cách hiệu quả trong tập lệnh của bạn.: in this tutorial, you will learn about the PHP overriding method and how to apply it effectively in your script.

Giới thiệu về phương pháp ghi đè PHP

Ghi đè phương thức cho phép một lớp trẻ cung cấp một triển khai cụ thể của một phương thức đã được cung cấp bởi lớp cha của nó.

Để ghi đè một phương thức, bạn xác định lại phương thức đó trong lớp con với cùng tên, tham số và loại trả về.

Phương thức trong lớp cha được gọi là phương thức ghi đè, trong khi phương thức trong lớp con được gọi là phương thức ghi đè. Mã trong phương thức ghi đè ghi đè (hoặc thay thế) mã trong phương thức ghi đè.overridden method, while the method in the child class is known as the overriding method. The code in the overriding method overrides (or replaces) the code in the overridden method.

PHP sẽ quyết định phương thức nào (phương thức ghi đè hoặc ghi đè) để gọi dựa trên đối tượng được sử dụng để gọi phương thức.

  • Nếu một đối tượng của lớp cha gọi phương thức, PHP sẽ thực thi phương thức ghi đè.
  • Nhưng nếu một đối tượng của lớp con gọi phương thức, PHP sẽ thực thi phương thức ghi đè.

Hãy để lấy một ví dụ để hiểu phương thức ghi đè tốt hơn.

Ví dụ sau đây xác định lớp

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)
1 có một phương thức công khai

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)
2 và lớp

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)
3 kế thừa lớp

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)
1:

<?php class Robot { public function greet() { return 'Hello!'; } } class Android extends Robot { }

Code language: HTML, XML (xml)

Khi bạn gọi phương thức

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)
2 thông qua phiên bản Android, PHP gọi phương thức

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)
2 của lớp

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)
1:

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)

Đây là một kịch bản kế thừa điển hình.

Đôi khi, bạn muốn thay thế hoàn toàn hành vi phương thức của lớp cha bằng một lớp mới. Trong trường hợp này, bạn cần ghi đè phương thức của lớp cha.

Để ghi đè một phương thức, bạn xác định lại phương thức trong lớp cha trong lớp con nhưng sử dụng logic khác.

Sau đây thêm phương thức

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)
2 vào lớp

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)
3 trả về một thông điệp lời chào khác nhau:

<?php class Robot { public function greet() { return 'Hello!'; } } class Android extends Robot { public function greet() { return 'Hi'; } } $robot = new Robot(); echo $robot->greet(); // Hello $android = new Android(); echo $android->greet(); // Hi!

Code language: HTML, XML (xml)

Làm thế nào nó hoạt động

  • Đầu tiên, gọi phương thức

    <?php $android = new Android(); echo $android->greet(); // Hello!

    Code language: HTML, XML (xml)
    2 của một thể hiện của lớp

    <?php $android = new Android(); echo $android->greet(); // Hello!

    Code language: HTML, XML (xml)
    1, phương thức

    <?php $android = new Android(); echo $android->greet(); // Hello!

    Code language: HTML, XML (xml)
    2 trong lớp

    <?php $android = new Android(); echo $android->greet(); // Hello!

    Code language: HTML, XML (xml)
    1 thực thi.
  • Thứ hai, hãy gọi phương thức

    <?php $android = new Android(); echo $android->greet(); // Hello!

    Code language: HTML, XML (xml)
    2 của một thể hiện của lớp

    <?php $android = new Android(); echo $android->greet(); // Hello!

    Code language: HTML, XML (xml)
    3, phương thức

    <?php $android = new Android(); echo $android->greet(); // Hello!

    Code language: HTML, XML (xml)
    2 trong lớp

    <?php $android = new Android(); echo $android->greet(); // Hello!

    Code language: HTML, XML (xml)
    3 thực thi.

Biểu đồ lớp sau đây minh họa mối quan hệ giữa các lớp

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)
1 và

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)
3:

Hướng dẫn can you override methods in php? - bạn có thể ghi đè các phương thức trong php không?

Gọi phương thức ghi đè trong phương thức ghi đè

Khi bạn ghi đè một phương thức, bạn sẽ có hai phiên bản của cùng một phương thức: một trong lớp cha và một trong lớp con.

Nếu bạn gọi phương thức của lớp cha trong phương thức trong lớp con, bạn không thể sử dụng từ khóa

<?php class Android extends Robot { public function greet() { $greeting = $this->greet(); return $greeting . ' from Android.'; } }

Code language: HTML, XML (xml)
0 như thế này:

<?php class Android extends Robot { public function greet() { $greeting = $this->greet(); return $greeting . ' from Android.'; } }

Code language: HTML, XML (xml)

<?php class Android extends Robot { public function greet() { $greeting = $this->greet(); return $greeting . ' from Android.'; } }

Code language: HTML, XML (xml)
1 sẽ tự gọi mình là vô thời hạn.

Để gọi phương thức

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)
2 của lớp

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)
1, bạn cần sử dụng

<?php class Android extends Robot { public function greet() { $greeting = $this->greet(); return $greeting . ' from Android.'; } }

Code language: HTML, XML (xml)
4 với toán tử phân giải phạm vi

<?php class Android extends Robot { public function greet() { $greeting = $this->greet(); return $greeting . ' from Android.'; } }

Code language: HTML, XML (xml)
5) như sau:

<?php class Android extends Robot { public function greet() { $greeting = parent::greet(); return $greeting . ' from Android.'; } }

Code language: HTML, XML (xml)

Trong ví dụ này, phương thức

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)
2 trong lớp Andoird gọi phương thức

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)
2 của lớp

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)
1. Nó kết hợp chuỗi được trả về bằng phương pháp

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)
2 của phương thức

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)
1 với chuỗi theo nghĩa đen

<?php class Android extends Robot { public function greet() { $greeting = parent::greet(); return $greeting . ' from Android.'; } }

Code language: HTML, XML (xml)
1 và trả về chuỗi được nối.

Thêm về phương pháp ghi đè PHP

Giả sử rằng bạn cần xác định một lớp

<?php class Android extends Robot { public function greet() { $greeting = parent::greet(); return $greeting . ' from Android.'; } }

Code language: HTML, XML (xml)
2 mới mở rộng lớp

<?php class Android extends Robot { public function greet() { $greeting = parent::greet(); return $greeting . ' from Android.'; } }

Code language: HTML, XML (xml)
3. Sau đây xác định lớp

<?php class Android extends Robot { public function greet() { $greeting = parent::greet(); return $greeting . ' from Android.'; } }

Code language: HTML, XML (xml)
3:

<?php class BankAccount { private $balance; public function __construct($amount) { $this->balance = $amount; } public function getBalance() { return $this->balance; } public function deposit($amount) { if ($amount > 0) { $this->balance += $amount; } return $this; } public function withdraw($amount) { if ($amount > 0 && $amount <= $this->balance) { $this->balance -= $amount; return true; } return false; } }

Code language: HTML, XML (xml)

Phương pháp

<?php class Android extends Robot { public function greet() { $greeting = parent::greet(); return $greeting . ' from Android.'; } }

Code language: HTML, XML (xml)
5 kiểm tra xem số tiền rút tiền lớn hơn 0 và nhỏ hơn hoặc bằng số dư hiện tại trước khi khấu trừ nó từ số dư.

Thứ hai, xác định lớp

<?php class Android extends Robot { public function greet() { $greeting = parent::greet(); return $greeting . ' from Android.'; } }

Code language: HTML, XML (xml)
2 kế thừa lớp

<?php class Android extends Robot { public function greet() { $greeting = parent::greet(); return $greeting . ' from Android.'; } }

Code language: HTML, XML (xml)
3. Lớp

<?php class Android extends Robot { public function greet() { $greeting = parent::greet(); return $greeting . ' from Android.'; } }

Code language: HTML, XML (xml)
2 cũng có phương thức

<?php class Android extends Robot { public function greet() { $greeting = parent::greet(); return $greeting . ' from Android.'; } }

Code language: HTML, XML (xml)
5 ghi đè phương thức

<?php class Android extends Robot { public function greet() { $greeting = parent::greet(); return $greeting . ' from Android.'; } }

Code language: HTML, XML (xml)
5 của lớp

<?php class Android extends Robot { public function greet() { $greeting = parent::greet(); return $greeting . ' from Android.'; } }

Code language: HTML, XML (xml)
3:

<?php class CheckingAccount extends BankAccount { private $minBalance; public function __construct($amount, $minBalance) { if ($amount > 0 && $amount >= $minBalance) { parent::__construct($amount); $this->minBalance = $minBalance; } else { throw new InvalidArgumentException('amount must be more than zero and higher than the minimum balance'); } } public function withdraw($amount) { $canWithdraw = $amount > 0 && $this->getBalance() - $amount > $this->minBalance; if ($canWithdraw) { parent::withdraw($amount); return true; } return false; } }

Code language: HTML, XML (xml)

Phương pháp

<?php class Android extends Robot { public function greet() { $greeting = parent::greet(); return $greeting . ' from Android.'; } }

Code language: HTML, XML (xml)
5 trong lớp

<?php class Android extends Robot { public function greet() { $greeting = parent::greet(); return $greeting . ' from Android.'; } }

Code language: HTML, XML (xml)
2 kiểm tra số tiền rút tiền so với số dư tối thiểu trước khi khấu trừ nó.

Biểu đồ lớp sau đây minh họa mối quan hệ giữa các lớp BankAccount và CheckingAccount:

Phương pháp cuối cùng

Để ngăn phương thức trong lớp con ghi đè phương thức trong lớp cha, bạn có thể đặt tiền tố phương thức với từ khóa

<?php class BankAccount { private $balance; public function __construct($amount) { $this->balance = $amount; } public function getBalance() { return $this->balance; } public function deposit($amount) { if ($amount > 0) { $this->balance += $amount; } return $this; } public function withdraw($amount) { if ($amount > 0 && $amount <= $this->balance) { $this->balance -= $amount; return true; } return false; } }

Code language: HTML, XML (xml)
4:

public final function methodName() { //... }

Code language: PHP (php)

Sau đây thêm phương thức

<?php class BankAccount { private $balance; public function __construct($amount) { $this->balance = $amount; } public function getBalance() { return $this->balance; } public function deposit($amount) { if ($amount > 0) { $this->balance += $amount; } return $this; } public function withdraw($amount) { if ($amount > 0 && $amount <= $this->balance) { $this->balance -= $amount; return true; } return false; } }

Code language: HTML, XML (xml)
5 vào lớp robot:

class Robot { public function greet() { return 'Hello!'; } final public function id() { return uniqid(); } }

Code language: PHP (php)

Nếu bạn cố gắng ghi đè phương thức

<?php class BankAccount { private $balance; public function __construct($amount) { $this->balance = $amount; } public function getBalance() { return $this->balance; } public function deposit($amount) { if ($amount > 0) { $this->balance += $amount; } return $this; } public function withdraw($amount) { if ($amount > 0 && $amount <= $this->balance) { $this->balance -= $amount; return true; } return false; } }

Code language: HTML, XML (xml)
5 từ lớp

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)
3, bạn sẽ gặp lỗi. Ví dụ:

class Android extends Robot { public function greet() { $greeting = parent::greet(); return $greeting . ' from Andoid.'; } public function id() { return uniqid('Android-'); } }

Code language: PHP (php)

Error:

<?php $android = new Android(); echo $android->greet(); // Hello!

Code language: HTML, XML (xml)
0

Bản tóm tắt

  • Ghi đè phương thức cho phép một lớp con xác định một phương thức ghi đè (hoặc thay thế) phương thức đã được cung cấp bởi lớp cha của nó.
  • Sử dụng

    <?php class BankAccount { private $balance; public function __construct($amount) { $this->balance = $amount; } public function getBalance() { return $this->balance; } public function deposit($amount) { if ($amount > 0) { $this->balance += $amount; } return $this; } public function withdraw($amount) { if ($amount > 0 && $amount <= $this->balance) { $this->balance -= $amount; return true; } return false; } }

    Code language: HTML, XML (xml)
    8 để gọi phương thức ghi đè trong phương thức ghi đè.
  • Sử dụng phương pháp cuối cùng khi bạn không muốn một phương pháp lớp con để ghi đè một phương thức lớp cha mẹ.

Bạn có thấy hướng dẫn này hữu ích không?

Bạn có thể ghi đè một phương thức?

Ghi đè phương thức chính Bạn không thể ghi đè các phương thức tĩnh và vì phương thức static void () () tĩnh công khai là tĩnh, chúng tôi không thể ghi đè nó.You cannot override static methods and since the public static void main() method is static we cannot override it.

Chúng ta có thể ghi đè hai phương thức không?

Do đó, bạn không thể ghi đè hai phương thức tồn tại trong cùng một lớp, bạn có thể quá tải chúng.you cannot override two methods that exist in the same class, you can just overload them.

Phương pháp nào không thể ghi đè?

Các phương thức Một lớp con cũng không thể ghi đè, một lớp con không thể ghi đè các phương thức được khai báo tĩnh trong siêu lớp.Nói cách khác, một lớp con không thể ghi đè một phương thức lớp.Xem ví dụ và các thành viên lớp để giải thích các phương pháp lớp.methods that are declared static in the superclass. In other words, a subclass cannot override a class method. See Instance and Class Members for an explanation of class methods.

Chúng ta có thể ghi đè phương thức ghi đè không?

Có, chúng ta có thể ghi đè một phương thức bị quá tải trong siêu lớp..