Answer the question
In order to leave comments, you need to log in
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
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).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question