Hướng dẫn method overriding in php example - ghi đè phương thức trong ví dụ php

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc
    Function Overloading: Function overloading contains same function name and that function performs different task according to number of arguments. For example, find the area of certain shapes where radius are given then it should return area of circle if height and width are given then it should give area of rectangle and others. Like other OOP languages function overloading can not be done by native approach. In PHP function overloading is done with the help of magic function __call(). This function takes function name and arguments. 
    Example: 
     

    Bàn luận

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

    Code language: HTML, XML (xml)
    1

    Quá tải chức năng và ghi đè là tính năng OOPS trong PHP. Trong quá tải chức năng, nhiều hơn một hàm có thể có cùng một chữ ký phương thức nhưng số lượng đối số khác nhau. Nhưng trong trường hợp ghi đè chức năng, nhiều hàm sẽ có cùng chữ ký phương thức và số lượng đối số. & NBSP; quá tải chức năng: quá tải chức năng chứa cùng tên hàm và hàm đó thực hiện tác vụ khác nhau theo số lượng đối số. Ví dụ: tìm diện tích của các hình dạng nhất định trong đó bán kính được đưa ra thì nó sẽ quay lại diện tích của vòng tròn nếu chiều cao và chiều rộng được đưa ra thì nó sẽ cho diện tích hình chữ nhật và các khu vực khác. Giống như các ngôn ngữ OOP khác không thể thực hiện được bằng cách tiếp cận bản địa. 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 (). Hàm này lấy tên chức năng và đối số. & Nbsp; ví dụ: & nbsp; & nbsp;

    PHP

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

    Code language: HTML, XML (xml)
    2

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

    Code language: HTML, XML (xml)
    3

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

    Code language: HTML, XML (xml)
    4

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

    Code language: HTML, XML (xml)
    5

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

    Code language: HTML, XML (xml)
    6

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

    Code language: HTML, XML (xml)
    7

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

    Code language: HTML, XML (xml)
    8

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

    Code language: HTML, XML (xml)
    9

    <?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)
    0

    <?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)
    1

    <?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)
    2223

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

    Code language: HTML, XML (xml)
    7

    <?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)
    5

    <?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)
    6

    <?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)
    0

    <?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)
    8

    <?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)
    9

    <?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)
    3

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

    Code language: HTML, XML (xml)
    1

    <?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)
    3

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

    Code language: HTML, XML (xml)
    9

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

    Code language: HTML, XML (xml)
    4

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

    Code language: HTML, XML (xml)
    5

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

    Code language: HTML, XML (xml)
    6

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

    Code language: HTML, XML (xml)
    7

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

    Code language: HTML, XML (xml)
    8

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

    Code language: HTML, XML (xml)
    9

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

    Code language: HTML, XML (xml)
    0

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

    Code language: HTML, XML (xml)
    9

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

    Code language: HTML, XML (xml)
    2

    <?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)
    8

    <?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)
    3

    <?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)
    1

    <?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)
    3

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

    Code language: HTML, XML (xml)
    4

    <?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)
    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)
    3

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

    Code language: HTML, XML (xml)
    5

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

    Code language: HTML, XML (xml)
    6

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

    Code language: HTML, XML (xml)
    5

    <?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)
    3

    <?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)
    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)
    9

    <?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)
    6

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

    Code language: HTML, XML (xml)
    8

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

    Code language: HTML, XML (xml)
    9

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

    Code language: HTML, XML (xml)
    9

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

    Code language: HTML, XML (xml)
    9

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

    Code language: HTML, XML (xml)
    9

    <?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)
    1

    <?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)
    9

    <?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)
    0

    <?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)
    1

    <?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)
    2

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

    Code language: PHP (php)
    4

    <?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)
    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)
    8

    <?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)
    9
    Function overriding is same as other OOPs programming languages. In function overriding, both parent and child classes should have same function name with and number of arguments. It is used to replace parent method in child class. The purpose of overriding is to change the behavior of parent class method. The two methods with the same name and same parameter is called overriding.
    Example: 
     

    Bàn luận

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

    Code language: HTML, XML (xml)
    1

    Quá tải chức năng và ghi đè là tính năng OOPS trong PHP. Trong quá tải chức năng, nhiều hơn một hàm có thể có cùng một chữ ký phương thức nhưng số lượng đối số khác nhau. Nhưng trong trường hợp ghi đè chức năng, nhiều hàm sẽ có cùng chữ ký phương thức và số lượng đối số. & NBSP; quá tải chức năng: quá tải chức năng chứa cùng tên hàm và hàm đó thực hiện tác vụ khác nhau theo số lượng đối số. Ví dụ: tìm diện tích của các hình dạng nhất định trong đó bán kính được đưa ra thì nó sẽ quay lại diện tích của vòng tròn nếu chiều cao và chiều rộng được đưa ra thì nó sẽ cho diện tích hình chữ nhật và các khu vực khác. Giống như các ngôn ngữ OOP khác không thể thực hiện được bằng cách tiếp cận bản địa. 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 (). Hàm này lấy tên chức năng và đối số. & Nbsp; ví dụ: & nbsp; & nbsp;

    PHP

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

    Code language: HTML, XML (xml)
    2

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

    Code language: HTML, XML (xml)
    3

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

    Code language: HTML, XML (xml)
    4

    <?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)
    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)
    3

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

    Code language: HTML, XML (xml)
    4

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

    Code language: HTML, XML (xml)
    5

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

    Code language: HTML, XML (xml)
    6

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

    Code language: HTML, XML (xml)
    7

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

    Code language: HTML, XML (xml)
    8

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

    Code language: HTML, XML (xml)
    9

    <?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)
    0

    PHP

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

    Code language: HTML, XML (xml)
    2

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

    Code language: HTML, XML (xml)
    3

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

    Code language: HTML, XML (xml)
    4

    <?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)
    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)
    3

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

    Code language: HTML, XML (xml)
    4

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

    Code language: HTML, XML (xml)
    5

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

    Code language: HTML, XML (xml)
    6

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

    Code language: HTML, XML (xml)
    7

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

    Code language: HTML, XML (xml)
    8

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

    Code language: HTML, XML (xml)
    9

    <?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)
    0

    <?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)
    1

    <?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)
    2223

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

    Code language: HTML, XML (xml)
    7

    <?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)
    5

    <?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)
    6

    <?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)
    0

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

    Code language: HTML, XML (xml)
    02

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

    Code language: HTML, XML (xml)
    11

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

    Code language: HTML, XML (xml)
    06

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

    Code language: HTML, XML (xml)
    11

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

    Code language: PHP (php)
    4

    <?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)
    8

    <?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)
    9

    <?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)
    3

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

    Code language: HTML, XML (xml)
    1

    <?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)
    3

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

    Code language: HTML, XML (xml)
    9

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

    Code language: HTML, XML (xml)
    4


    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)
    15 có một phương thức công khai

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

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

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

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

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

    Code language: HTML, XML (xml)
    15:

    <?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)
    16 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)
    16 của lớp

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

    Code language: HTML, XML (xml)
    15:

    <?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)
    16 vào lớp

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

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

    <?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)
      16 của một thể hiện của lớp

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

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

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

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

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

      Code language: HTML, XML (xml)
      15 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)
      16 của một thể hiện của lớp

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

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

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

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

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

      Code language: HTML, XML (xml)
      17 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)
    15 và

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

    Code language: HTML, XML (xml)
    17:

    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 $android = new Android(); echo $android->greet(); // Hello!

    Code language: HTML, XML (xml)
    34 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 $android = new Android(); echo $android->greet(); // Hello!

    Code language: HTML, XML (xml)
    35 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)
    16 của lớp

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

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

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

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

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

    Code language: HTML, XML (xml)
    39) 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)
    16 trong lớp Andoird gọi phương thức

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

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

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

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

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

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

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

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

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

    Code language: HTML, XML (xml)
    45 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 $android = new Android(); echo $android->greet(); // Hello!

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

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

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

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

    Code language: HTML, XML (xml)
    47:

    <?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 $android = new Android(); echo $android->greet(); // Hello!

    Code language: HTML, XML (xml)
    49 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 $android = new Android(); echo $android->greet(); // Hello!

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

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

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

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

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

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

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

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

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

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

    Code language: HTML, XML (xml)
    47:

    <?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 $android = new Android(); echo $android->greet(); // Hello!

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

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

    Code language: HTML, XML (xml)
    46 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:

    Hướng dẫn method overriding in php example - ghi đè phương thức trong ví dụ php

    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 $android = new Android(); echo $android->greet(); // Hello!

    Code language: HTML, XML (xml)
    58:

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

    Code language: PHP (php)

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

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

    Code language: HTML, XML (xml)
    59 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 $android = new Android(); echo $android->greet(); // Hello!

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

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

    Code language: HTML, XML (xml)
    17, 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 $android = new Android(); echo $android->greet(); // Hello!

      Code language: HTML, XML (xml)
      62 để 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 ghi đè được giải thích với ví dụ trong PHP là gì?

    Trong quá trình ghi đè chức năng, cả hai lớp cha và con nên có cùng tên hàm và số lượng đối số. Nó được sử dụng để thay thế phương thức cha mẹ trong lớp con. Mục đích của việc ghi đè là thay đổi hành vi của phương pháp lớp cha. Hai phương thức có cùng tên và cùng một tham số được gọi là ghi đè.both parent and child classes should have same function name with and number of arguments. It is used to replace parent method in child class. The purpose of overriding is to change the behavior of parent class method. The two methods with the same name and same parameter is called overriding.

    Phương pháp có thể ghi đè trong PHP 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.

    Phương pháp ghi đè lên ví dụ thời gian thực là gì?

    Nhiều thói quen trong thế giới thực của một đứa trẻ cha mẹ có thể bị ghi đè trong tự nhiên.Ví dụ, một đứa trẻ cha mẹ có thể có một hành vi phổ biến hát, trong đó trong hành vi ca hát của một đứa trẻ có thể khác với cha mẹ của mình.Một người có thể là ca sĩ cổ điển trong khi người khác là một ca sĩ nhạc rock.a parent child can have a common behavior singing, where in the singing behavior of a child may be different than his parent. One may be classical singer while other is a rock singer.

    Làm thế nào để PHP thực hiện các phương thức thừa kế vượt trội?

    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 đè.If an object of the parent class invokes the method, PHP will execute the overridden method. But if an object of the child class invokes the method, PHP will execute the overriding method.