在包含之后扩展
我对某些事情感到困惑,写下我自己的代码有点复杂。
但我会尽力解释它。
我有一个功能类似于引导程序的文件。
在文件的开头,我包含了一个名为 settings 的类的设置 php。
紧接着,我实例化设置类。
之后我添加了一个 php 文档,它将实例化系统中的所有其他类。
代码:
include('settings.php');
$settings = new Settings();
include('classes.php');
设置类内部有一个构造函数,它将运行许多函数,这些函数将设置一些常量变量作为默认值。
现在我的问题来了。
例如,假设在classes.php 中实例化了“Test”类。 这意味着我可以从设置类扩展该类。
但是当我这样做时,构造函数将再次运行并收到常量变量已定义的错误。
我的问题是......实例化测试类时,php 会运行设置类的构造吗?
问候。
I'm puzzled about something and its a little complicated to put down my own code.
BUT I will do my best to explain it.
I have a file that functions like a bootstrap.
At the start of the file I include a settings php with a class called settings.
Directly after it, I instantiate the settings class.
After it i include a php document that will instantiate all other classes in my system.
code:
include('settings.php');
$settings = new Settings();
include('classes.php');
Inside the settings class there is a constructor that will run a number of functions that will set some constance variables for default values.
Now comes my problem.
As example lets say that in the classes.php a class 'Test' is instantiated.
That would mean i can extend that class from the settings class.
But when i do that the constructor will run again and get errors that the constance vars are already defined.
My question is... Will php run the construct of the settings class when instantiating the test class?
Regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
Test
类有自己的构造函数(_construct
方法),则不会调用父构造函数,除非您从子类构造函数内部显式调用它,例如parent::__construct ()
。如果
Test
类没有__construct
方法,则将调用父构造函数(如果有)。If
Test
class has its own constructor (_construct
method) then parent constructor will not be invoked unless you call it explicitly from inside child class constructor likeparent::__construct()
.If
Test
class does not have__construct
method then parent constructor will be called (if any).