Hướng dẫn how can i override in php? - làm cách nào tôi có thể ghi đè lên trong php?

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 how can i override in php? - làm cách nào tôi có thể ghi đè lên trong php?

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?

Làm thế nào bạn có thể ghi đè một phương thức?

Trong Java, phương thức ghi đè xảy ra khi lớp con (lớp con) có cùng phương thức với lớp cha. Nói cách khác, phương thức ghi đè xảy ra khi một lớp con cung cấp một triển khai cụ thể của một phương thức được khai báo bởi một trong các lớp cha của nó.when a subclass (child class) has the same method as the parent class. In other words, method overriding occurs when a subclass provides a particular implementation of a method declared by one of its parent classes.

Làm thế nào chúng ta có thể ngừng ghi đè trong PHP?

Tránh ghi đè phương thức php class a {final function display () {echo "Inside Class A";}} lớp B mở rộng {function display () {echo "bên trong lớp B";}}?> Vì vậy, sử dụng từ khóa cuối cùng của tên phương thức ngăn không cho nó bị quá tải.using the keyword final infront of the method name prevents it from being overriden.

Thuộc tính ghi đè trong PHP là gì?

Ghi đè là một khái niệm lập trình hướng đối tượng tương tự như một khái niệm như lớp, đối tượng, đóng gói, đa hình, quá tải, vv trong PHP.Việc ghi đè các hàm và lớp được thực hiện khi một phương thức trong lớp dẫn xuất được tạo giống như phương thức trong lớp cơ sở hoặc lớp cha.an Object-Oriented Programming concept that is similar to a concept like Class, Object, Encapsulation, Polymorphism, Overloading etc in PHP. Overriding of functions and classes are done when a method in the derived class is created which is the same as that of the method in the base class or parent class.

Làm thế nào tôi có thể quá tải trong PHP?

Trong quá tải chức năng PHP được thực hiện với sự trợ giúp của hàm ma thuật __call () ...
Tất cả các phương thức quá tải phải được định nghĩa là công khai ..
Sau khi tạo đối tượng cho một lớp, chúng ta có thể truy cập một tập hợp các thực thể là thuộc tính hoặc phương thức không được xác định trong phạm vi của lớp ..