Câu hỏi phỏng vấn PHP
Câu hỏi

Are Parent constructors called implicitly inside a class constructor?

Câu trả lời

In PHP, parent constructors are not called implicitly inside a class constructor. If you want to call a parent constructor within a child class, you need to do so explicitly using the parent::__construct() syntax. Here’s an example to illustrate this:

php Copy
<?php
class ParentClass {
    public function __construct() {
        echo "Parent constructor called\n";
    }
}

class ChildClass extends ParentClass {
    public function __construct() {
        // Explicitly calling the parent constructor
        parent::__construct();
        echo "Child constructor called\n";
    }
}

// Creating an instance of ChildClass
$child = new ChildClass();
?>

Output:

Copy
Parent constructor called
Child constructor called

In this example, the ChildClass co...

middle

middle

Gợi ý câu hỏi phỏng vấn

junior

Explain usage of enumerations in PHP

middle

What are some of the big changes PHP has gone through in the past few years?

senior

Explain the Order of Precedence for Traits in PHP

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào