Reputation: 1995
I've been sitting on this for ages now and cannot figure out a way that satisfies me.
My problem: I got a parent class and various subclasses. In the constructor of the parent, a new object is created (amongst other things). This object is from my log file class. It is stored in a protected variable.
In the parent class, the log keeping is not a problem, everything is done according to plan.
If I want to use the object in a subclass, I cant get it to work. I just cant access it. I tried a function in the parent class in order to return the object, this didnt work either.
I am at a loss here.
This is the constructor of the parent:
function __construct() {
...
$this->reg_log_file = new log_file(bef_registration::REG_LOG_NAME, bef_registration::REG_LOG_KEEPING);
...
}
In order to create a new entry, in the parent class, the following is sufficient:
$this->reg_log_file->entry('start log');
In a child class, this would lead to an error:
Call to a member function entry() on a non-object
After googling and reading for hours, I still havent come up with a proper way of doing this.
Anyone with an idea?
Upvotes: 0
Views: 216
Reputation: 11071
You should run parent::__construct();
in the body of every child contstructor and the log object will be instantiated and visible in all inherited subclasses.
Upvotes: 1