Hướng dẫn is method overriding possible in php? - có thể ghi đè 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

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

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

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

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

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

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

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

Code language: HTML, XML (xml)
1:

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

$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

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

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

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

Code language: HTML, XML (xml)
1:

$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

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

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

$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:

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

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

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

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

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

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

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

    $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

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

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

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

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

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

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

    $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

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

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

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

Code language: HTML, XML (xml)
3:

Hướng dẫn is method overriding possible in php? - có thể ghi đè 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

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

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

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

Code language: HTML, XML (xml)

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

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

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

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

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

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

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

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

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

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

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

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

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

$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

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

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

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

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

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

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

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

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

Code language: HTML, XML (xml)
3:

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

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

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

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

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

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

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

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

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

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

Code language: HTML, XML (xml)
3:

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

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

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

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

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

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

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

$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:

$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

    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?

Phương pháp hỗ trợ PHP có ghi đè không?

PHP không hỗ trợ quá tải phương pháp. Trong trường hợp bạn chưa bao giờ nghe nói về quá tải phương thức, điều đó có nghĩa là ngôn ngữ có thể chọn một phương thức dựa trên các tham số bạn đang sử dụng để gọi nó. Điều này là có thể trong nhiều ngôn ngữ lập trình khác như Java, C ++.. In case you've never heard of method overloading, it means that the language can pick a method based on which parameters you're using to call it. This is possible in many other programming languages like Java, C++.

Tại sao không thể quá tải phương thức trong PHP?

Bạn không thể quá tải các chức năng PHP.Chữ ký chức năng chỉ dựa trên tên của chúng và không bao gồm danh sách đối số, vì vậy bạn không thể có hai hàm cùng tên.Quá tải phương pháp lớp khác nhau trong PHP so với nhiều ngôn ngữ khác.PHP sử dụng cùng một từ nhưng nó mô tả một mẫu khác.Function signatures are based only on their names and do not include argument lists, so you cannot have two functions with the same name. Class method overloading is different in PHP than in many other languages. PHP uses the same word but it describes a different pattern.

Làm thế nào chúng ta có thể đạt được quá tải phương pháp trong PHP?

Để đạt được quá tải phương pháp trong PHP, chúng ta phải sử dụng các phương thức ma thuật của PHP __call () để đạt được quá tải phương pháp.__call (): Trong PHP, nếu một lớp thực thi __call () và nếu một đối tượng của lớp đó được gọi bằng một phương thức không tồn tại thì __call () được gọi thay vì phương thức đó.

PHP có hỗ trợ ghi đè chức năng cha mẹ không?

Ghi đè chỉ phù hợp với các lớp dẫn xuất, trong đó lớp cha đã xác định một phương thức và lớp dẫn xuất muốn ghi đè phương thức đó.Trong PHP, bạn chỉ có thể quá tải các phương thức bằng phương pháp ma thuật __call.you can only overload methods using the magic method __call .