Hướng dẫn call parent class constructor php - gọi hàm tạo lớp cha php

Tóm tắt: Trong hướng dẫn này, bạn sẽ học cách gọi hàm tạo cha mẹ từ hàm tạo của lớp con.: in this tutorial, you’ll learn how to call the parent constructor from the constructor of the child class.

Lớp trẻ không có một người xây dựng

Trong hướng dẫn kế thừa, bạn đã học được cách xác định lớp SavingAccount kế thừa lớp

<?php class SavingAccount extends BankAccount { private $interestRate; public function setInterestRate($interestRate) { $this->interestRate = $interestRate; } public function addInterest() { // calculate interest $interest = $this->interestRate * $this->getBalance(); // deposit interest to the balance $this->deposit($interest); } }

Code language: HTML, XML (xml)
0:

Hướng dẫn call parent class constructor php - gọi hàm tạo lớp cha php

Tuy nhiên, chúng tôi đã thảo luận về các nhà xây dựng của các lớp phụ huynh và con cái trong bối cảnh thừa kế.

Sau đây thêm một hàm tạo vào lớp

<?php class SavingAccount extends BankAccount { private $interestRate; public function setInterestRate($interestRate) { $this->interestRate = $interestRate; } public function addInterest() { // calculate interest $interest = $this->interestRate * $this->getBalance(); // deposit interest to the balance $this->deposit($interest); } }

Code language: HTML, XML (xml)
0, chấp nhận tham số

<?php class SavingAccount extends BankAccount { private $interestRate; public function setInterestRate($interestRate) { $this->interestRate = $interestRate; } public function addInterest() { // calculate interest $interest = $this->interestRate * $this->getBalance(); // deposit interest to the balance $this->deposit($interest); } }

Code language: HTML, XML (xml)
2. Trình xây dựng gán đối số

<?php class SavingAccount extends BankAccount { private $interestRate; public function setInterestRate($interestRate) { $this->interestRate = $interestRate; } public function addInterest() { // calculate interest $interest = $this->interestRate * $this->getBalance(); // deposit interest to the balance $this->deposit($interest); } }

Code language: HTML, XML (xml)
2 cho thuộc tính

<?php class SavingAccount extends BankAccount { private $interestRate; public function setInterestRate($interestRate) { $this->interestRate = $interestRate; } public function addInterest() { // calculate interest $interest = $this->interestRate * $this->getBalance(); // deposit interest to the balance $this->deposit($interest); } }

Code language: HTML, XML (xml)
2:

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

Code language: HTML, XML (xml)

Lớp SavingAccount vẫn giữ nguyên và không bao gồm hàm tạo riêng của nó:

<?php class SavingAccount extends BankAccount { private $interestRate; public function setInterestRate($interestRate) { $this->interestRate = $interestRate; } public function addInterest() { // calculate interest $interest = $this->interestRate * $this->getBalance(); // deposit interest to the balance $this->deposit($interest); } }

Code language: HTML, XML (xml)

Khi bạn tạo một thể hiện mới của SavingAccount, PHP sẽ gọi hàm tạo của lớp SavingAccount. Tuy nhiên, PHP không thể tìm thấy hàm tạo trong

<?php class SavingAccount extends BankAccount { private $interestRate; public function setInterestRate($interestRate) { $this->interestRate = $interestRate; } public function addInterest() { // calculate interest $interest = $this->interestRate * $this->getBalance(); // deposit interest to the balance $this->deposit($interest); } }

Code language: HTML, XML (xml)
8. Do đó, nó tiếp tục tìm kiếm hàm tạo của lớp cha của lớp SavingAccount, đó là lớp

<?php class SavingAccount extends BankAccount { private $interestRate; public function setInterestRate($interestRate) { $this->interestRate = $interestRate; } public function addInterest() { // calculate interest $interest = $this->interestRate * $this->getBalance(); // deposit interest to the balance $this->deposit($interest); } }

Code language: HTML, XML (xml)
0. Và nó gọi hàm tạo của lớp

<?php class SavingAccount extends BankAccount { private $interestRate; public function setInterestRate($interestRate) { $this->interestRate = $interestRate; } public function addInterest() { // calculate interest $interest = $this->interestRate * $this->getBalance(); // deposit interest to the balance $this->deposit($interest); } }

Code language: HTML, XML (xml)
0.

Nếu bạn không chuyển một đối số cho hàm tạo của lớp SavingAccount, bạn sẽ gặp lỗi:

$account = new SavingAccount();

Code language: PHP (php)

Error:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function BankAccount::__construct(), 0 passed in ... on line 5 and exactly 1 expected in ...

Code language: JavaScript (javascript)

Nhưng nếu bạn chuyển một đối số cho hàm tạo, nó sẽ hoạt động hoàn hảo:

$account = new SavingAccount(100);

Code language: PHP (php)

Xác định một hàm tạo trong lớp trẻ em

Một lớp trẻ em có thể có hàm tạo riêng của nó. Ví dụ: bạn có thể thêm một hàm tạo vào lớp SavingAccount như sau:

<?php class SavingAccount extends BankAccount { private $interestRate; public function __construct($interestRate) { $this->interestRate = $interestRate; } public function setInterestRate($interestRate) { $this->interestRate = $interestRate; } public function addInterest() { // calculate interest $interest = $this->interestRate * $this->getBalance(); // deposit interest to the balance $this->deposit($interest); } }

Code language: HTML, XML (xml)

Lớp SavingAccount có một hàm tạo khởi tạo thuộc tính

$account = new SavingAccount();

Code language: PHP (php)
5.

Khi một lớp trẻ có hàm tạo riêng, hàm tạo trong lớp trẻ sẽ không gọi hàm xây dựng của lớp cha.

Ví dụ: sau đây tạo ra một thể hiện mới của lớp SavingAccount và khởi tạo thuộc tính

$account = new SavingAccount();

Code language: PHP (php)
7 thành giá trị

$account = new SavingAccount();

Code language: PHP (php)
8

$account = new SavingAccount(0.05);

Code language: PHP (php)

Để gọi hàm tạo của lớp cha từ hàm tạo của lớp con, bạn sử dụng cú pháp

$account = new SavingAccount();

Code language: PHP (php)
9.

Sau đây thay đổi hàm tạo của lớp SavingAccount chấp nhận hai đối số: số dư & lãi suất. Nó cũng gọi hàm tạo của lớp

<?php class SavingAccount extends BankAccount { private $interestRate; public function setInterestRate($interestRate) { $this->interestRate = $interestRate; } public function addInterest() { // calculate interest $interest = $this->interestRate * $this->getBalance(); // deposit interest to the balance $this->deposit($interest); } }

Code language: HTML, XML (xml)
0 để khởi tạo thuộc tính

<?php class SavingAccount extends BankAccount { private $interestRate; public function setInterestRate($interestRate) { $this->interestRate = $interestRate; } public function addInterest() { // calculate interest $interest = $this->interestRate * $this->getBalance(); // deposit interest to the balance $this->deposit($interest); } }

Code language: HTML, XML (xml)
2:

<?php class SavingAccount extends BankAccount { private $interestRate; public function __construct($balance, $interestRate) { parent::__construct($balance); $this->interestRate = $interestRate; } public function setInterestRate($interestRate) { $this->interestRate = $interestRate; } public function addInterest() { // calculate interest $interest = $this->interestRate * $this->getBalance(); // deposit interest to the balance $this->deposit($interest); } }

Code language: HTML, XML (xml)

Cú pháp để gọi hàm tạo cha mẹ giống như một phương thức thông thường.

Đặt nó tất cả cùng nhau:

<?php class BankAccount { private $balance; public function __construct($balance) { $this->balance = $balance; } public function getBalance() { return $this->balance; } public function deposit($amount) { if ($amount > 0) { $this->balance += $amount; } return $this; } } class SavingAccount extends BankAccount { private $interestRate; public function __construct($balance, $interestRate) { parent::__construct($balance); $this->interestRate = $interestRate; } public function setInterestRate($interestRate) { $this->interestRate = $interestRate; } public function addInterest() { $interest = $this->interestRate * $this->getBalance(); $this->deposit($interest); } } $account = new SavingAccount(100, 0.05); $account->addInterest(); echo $account->getBalance();

Code language: HTML, XML (xml)

Biểu đồ lớp sau đây minh họa sự kế thừa giữa các lớp SavingAccount

<?php class SavingAccount extends BankAccount { private $interestRate; public function setInterestRate($interestRate) { $this->interestRate = $interestRate; } public function addInterest() { // calculate interest $interest = $this->interestRate * $this->getBalance(); // deposit interest to the balance $this->deposit($interest); } }

Code language: HTML, XML (xml)
0:

Bản tóm tắt

  • Trình xây dựng của lớp con không tự động gọi hàm tạo của lớp cha.
  • Sử dụng

    $account = new SavingAccount();

    Code language: PHP (php)
    9 để gọi hàm tạo cha mẹ từ hàm tạo trong lớp con.

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

Làm thế nào chúng ta có thể gọi một hàm tạo lớp cha mẹ?

Để gọi hàm tạo của lớp cha, chúng ta có thể sử dụng từ khóa siêu. Phương thức Super () từ phương thức cấu trúc được sử dụng cho việc gọi phương thức cấu trúc của lớp cha để có quyền truy cập vào các thuộc tính và phương thức của cha mẹ.use the super keyword. The super() method from the constructor method is used for the invocation of the constructor method of the parent class to get access to the parent's properties and methods.

PHP có tự động gọi hàm tạo cha mẹ không?

Thực tế là PHP luôn gọi hàm xây dựng "gần nhất", nghĩa là nếu không có hàm tạo con nào, nó sẽ gọi hàm tạo cha mẹ chứ không phải nhà xây dựng ông bà, có nghĩa là chúng ta cần phải tự gọi cho hàm tạo cha mẹ.Chúng ta có thể làm điều này bằng cách sử dụng chức năng đặc biệt gọi cha mẹ :: __ construct ().PHP always calls the "nearest" constructor, that is if there is no child constructor it will call the parent constructor and not the grandparent constructor, means that we need to call the parent constructor ourselves. We can do this by using the special function call parent::__construct().

Làm thế nào để bạn gọi một hàm tạo lớp trong PHP?

Trình xây dựng cho phép bạn khởi tạo các thuộc tính của một đối tượng khi tạo đối tượng.Nếu bạn tạo hàm __construct (), PHP sẽ tự động gọi hàm này khi bạn tạo một đối tượng từ một lớp.Lưu ý rằng hàm xây dựng bắt đầu với hai dấu gạch dưới (__)!If you create a __construct() function, PHP will automatically call this function when you create an object from a class. Notice that the construct function starts with two underscores (__)!

Lớp con có gọi hàm tạo cha mẹ không?

Nếu lớp cha thực hiện một hàm tạo với các đối số và không có hàm tạo mà không có đối số, thì các hàm tạo con phải gọi một hàm tạo cha mẹ một cách rõ ràng..