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

Không, hàm tạo của lớp cha không được gọi nếu lớp con định nghĩa một hàm tạo.

Từ người xây dựng lớp con của bạn, bạn phải gọi hàm tạo của lớp phụ huynh:

parent::__construct();

Vượt qua các tham số của nó, nếu cần.

Nói chung, bạn sẽ làm như vậy khi bắt đầu hàm tạo của lớp con, trước bất kỳ mã cụ thể nào; có nghĩa là, trong trường hợp của bạn, bạn sẽ có:

class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }


Và, để tham khảo, bạn có thể xem trang này của Hướng dẫn sử dụng PHP: Chất xây dựng và Hàng không - nó nêu (trích dẫn):

Lưu ý: Các hàm tạo cha mẹ không được gọi là ngầm nếu lớp con định nghĩa một hàm tạo. Để chạy một hàm tạo cha mẹ, cần có một cuộc gọi đến

class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
1 trong hàm tạo con. Parent constructors are not called implicitly if the child class defines a constructor.
In order to run a parent constructor, a call to
class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
1 within the child constructor is required.

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

class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
2 kế thừa lớp
class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
3:

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

class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
3, chấp nhận tham số
class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
5. Trình xây dựng gán đối số
class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
5 cho thuộc tính
class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
5:

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

class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
2 vẫn giữ nguyên và không bao gồm hàm tạo riêng của nó:

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

class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
2, PHP sẽ gọi hàm tạo của lớp
class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
2. Tuy nhiên, PHP không thể tìm thấy hàm tạo trong

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)
1. Do đó, nó tiếp tục tìm kiếm hàm tạo của lớp cha của lớp
class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
2, đó là lớp
class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
3. Và nó gọi hàm tạo của lớp
class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
3.

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

class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
2, 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

class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
2 như sau:

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

class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
2 có một hàm tạo khởi tạo thuộc tính

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

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

class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
2 và khởi tạo thuộc tính

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 thành giá trị

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

$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

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.

Sau đây thay đổi hàm tạo của lớp

class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
2 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
class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
3 để khởi tạo thuộc tính
class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
5:

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:

class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
0

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

class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
2 và
class Child extends Base {
    function __construct() {
        parent::__construct();
        echo 'Child __construct
'; } }
3:

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

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

    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 để 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 hàm tạo cha mẹ trong PHP?

Để chạy một hàm tạo cha mẹ, một cuộc gọi đến cha mẹ :: __ construct () trong hàm tạo con là bắt buộc. Nếu trẻ không định nghĩa một hàm tạo thì nó có thể được kế thừa từ lớp cha giống như một phương thức lớp bình thường (nếu nó không được tuyên bố là riêng tư).a call to parent::__construct() within the child constructor is required. If the child does not define a constructor then it may be inherited from the parent class just like a normal class method (if it was not declared as private).

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ớp trừu tượng có thể có hàm tạo PHP không?

Giống như lớp trừu tượng C ++ hoặc Java trong PHP cũng có thể chứa hàm tạo.abstract class in PHP can contain constructor also.

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 hàm tạo 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.