简体   繁体   English

日志记录:在多个类中使用相同的对象

[英]php - Logging: use the same object in multiple classes

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 调用非对象上的成员函数entry()

After googling and reading for hours, I still havent come up with a proper way of doing this. 谷歌搜索和阅读了几个小时后,我仍然没有想出正确的方法来做到这一点。

Anyone with an idea? 有人有主意吗?

You should run parent::__construct(); 您应该运行parent::__construct(); in the body of every child contstructor and the log object will be instantiated and visible in all inherited subclasses. 在每个子构造函数的主体中,日志对象将被实例化并在所有继承的子类中可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM