Hàm gọi PHP từ lớp mở rộng

Bản 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 từ hàm tạo của lớp con

Lớp con không có hàm tạo

Trong hướng dẫn kế thừa, bạn đã học cách định nghĩa lớp SavingAccount kế thừa lớ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)
0

Tuy nhiên, chúng ta chưa thảo luận về hàm tạo của lớp cha và lớp con trong ngữ cảnh kế thừa

Phần sau thêm một hàm tạo vào lớ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)
0, lớp này chấp nhận tham số

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. Hàm tạo gán đối số

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

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

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 của chính 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 lớp 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

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

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 phương thức khởi tạo của lớ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)
0

Nếu bạn không truyền đố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)

Lỗi

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 đối số cho hàm tạo, nó sẽ hoạt động hoàn hảo

$account = new SavingAccount(100);

Code language: PHP (php)

Định nghĩa một hàm tạo trong lớp con

Một lớp con có thể có hàm tạo riêng. Ví dụ: bạn có thể thêm hàm tạo vào lớp SavingAccount 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 SavingAccount có hàm khởi tạo khởi tạo thuộc tính

$account = new SavingAccount();

Code language: PHP (php)
5

Khi một lớp con có hàm tạo riêng, hàm tạo của lớp con sẽ không tự động gọi hàm tạo của lớp cha

Ví dụ: phần sau tạo 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

Phần sau đây thay đổi hàm tạo của lớp SavingAccount chấp nhận hai đối số. cân bằng và lãi suất. Nó cũng gọi hàm tạo của lớ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)
0 để 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)
2

Làm cách nào để gọi phương thức lớp mở rộng trong PHP?

lớp một { var $b; . } }

Làm cách nào tôi có thể truy cập một hàm lớp trong PHP?

Bạn phải có một thể hiện của lớp đó để gọi nó , chẳng hạn. $widget = new bla_bla(); . la (); .

Làm cách nào để gọi phương thức của lớp cha trong 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 cha. Cú pháp __construct(đối số) . Cú pháp gọi hàm tạo cha giống như một phương thức thông thường.

Mục đích của $this và extends trong PHP là gì?

Định nghĩa và cách sử dụng . Cái này gọi là kế thừa. Một lớp dẫn xuất có tất cả các thuộc tính công khai và được bảo vệ của lớp mà nó được dẫn xuất từ. to derive a class from another class. This is called inheritance. A derived class has all of the public and protected properties of the class that it is derived from.