E
E
Evgeny Ivanov2021-11-10 09:42:47
PHP
Evgeny Ivanov, 2021-11-10 09:42:47

Why doesn't the class constructor work?

In the FilePath class, I form the path to the database file (I left one path, in reality there are more of them).
In the MyBase class, I use these paths.

But it looks like the base class constructor is not getting executed.
The word work is not displayed and when an instance of the MyBase class is created (and its constructor is called), the SQLiteFilePath field remains set to default
Why does the class constructor not work?

abstract class FilePath{

protected $SQLiteFilePath="default";

function __construct(){
echo "work"; // Не отображается
$this->SQLiteFilePath='base.db'; // Не присваивается полю выше
}}


class MyBase extends FilePath{

function __construct(){
echo $this->SQLiteFilePath; // default,. а должно быть base.db}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sand, 2021-11-10
@logpol32

Note: Constructors defined in parent classes are not automatically called if the child class defines its own constructor. To call a constructor declared in a parent class, call parent::__construct() inside the constructor of the child class. If a constructor is not defined in the child class, then it can be inherited from the parent class as a regular method (if it has not been defined as private).

Here is the original

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question