php PHP中在子构造函数之前调用父构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3079883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call parent constructor before child constructor in PHP
提问by Matt
I was wondering if its possible to call the parents __construct(), before the child's __construct() with inheritance in PHP.
我想知道是否可以在使用 PHP 继承的孩子的 __construct() 之前调用父母的 __construct()。
Example:
例子:
class Tag {
__construct() {
// Called first.
}
}
class Form extends Tag {
__construct() {
// Called second.
}
}
new Form();
Ideally, I would be able to do something in between them. If this is not possible, is there an alternative, which would allow me to do this?
理想情况下,我将能够在它们之间做一些事情。如果这是不可能的,是否有替代方法可以让我这样做?
The reason I want to do this is to be able to load a bunch of default settings specific to the Tag that Form can use when __construct() is called.
我想要这样做的原因是能够加载一堆特定于表单在调用 __construct() 时可以使用的标签的默认设置。
EDIT: Sorry forgot to add this.. I'd rather not call the parent class from the child class. It's simply because it exposes some private data (for the parent) to the child, when you pass it as an argument
编辑:抱歉忘了添加这个..我宁愿不从子类调用父类。这仅仅是因为当您将其作为参数传递时,它向孩子公开了一些私有数据(对于父级)
This is what I want to do:
这就是我想要做的:
$tag = new Tag($privateInfo, $publicInfo);
$tag->extend(new Form()); // Ideal function, prob doesn't work with inheritance.
Tag.php
标签.php
class Tag {
private $privateInfo;
public $publicInfo;
__construct($private, $public) {
$this->privateInfo = $private;
$this->publicInfo = $public;
}
}
Form.php
表单.php
class Form extends Tag {
__construct() {
echo $this->publicInfo;
}
}
Make sense?
有道理?
Thanks! Matt Mueller
谢谢!马特·穆勒
回答by Denis 'Alpheus' Cahuk
Just call parent::__construct in the child.
只需在孩子中调用 parent::__construct 即可。
class Form extends Tag
{
function __construct()
{
parent::__construct();
// Called second.
}
}
回答by Pentium10
yeah just call parent::__construct()in your construct
是的,只需调用parent::__construct()您的构造
回答by Artefacto
Yes, but only internally (i.e., by writing a PHP extension), so if I were you I'd settle with calling parent::__construct(). See this sectionon the PHP wiki.
是的,但仅限于内部(即,通过编写 PHP 扩展),所以如果我是你,我会解决调用parent::__construct(). 请参阅PHP wiki 上的这一部分。
Sorry, PHP is not Java. I think not requiring (implicitly or explictly) the super constructor to be called was a very poor design decision.
抱歉,PHP 不是 Java。我认为不需要(隐式或显式)调用超级构造函数是一个非常糟糕的设计决策。
回答by Reece45
From the sounds of it you may want to rethink your design so that you don't need to pass the parameters in the constructor. If you don't think it can be done, ask it as a question, you might be surprised by some of the suggestions.
从它的声音来看,您可能想要重新考虑您的设计,以便您不需要在构造函数中传递参数。如果您认为它无法完成,请将其作为问题提出,您可能会对某些建议感到惊讶。
The child class has the ability to override the parent constructor without calling it at all. I would recommend having a final method in the parent class. That way everyone knows you don't want this being overriden, and any inherited class (rightly) has access to do whatever it wants in the constructor.
子类能够覆盖父构造函数而根本不调用它。我建议在父类中有一个 final 方法。这样每个人都知道你不希望它被覆盖,任何继承的类(正确地)都可以在构造函数中做任何它想做的事情。
class Father {
private $_privateData;
final function setPrivateData($privateData) {
$this->_privateData = $privateData;
}
}
Another, not recommended, more "reinventing the wheel", solution would be to define a function in the parent class, say _construct(), that's called in its own construct. Its not really clear, doesn't use language features/constructs, and is very specific to a single application.
另一个不推荐的、更“重新发明轮子”的解决方案是在父类中定义一个函数,比如 _construct(),它在自己的构造中被调用。它不是很清楚,不使用语言功能/结构,并且非常特定于单个应用程序。
One last thing to keep in mind: you can't really hide information from the child class. With Reflection, serialize, var_dump, var_export and all these other convenient APIs in the php language, if there is code that shouldn't do anything with the data, then there's not really much you can do asides from not store it. There are libraries and such that help create sandboxes, but its hard to sandbox an object from itself.
要记住的最后一件事:您无法真正向子类隐藏信息。使用反射、序列化、var_dump、var_export 和 php 语言中的所有其他方便的 API,如果有代码不应该对数据做任何事情,那么除了不存储它之外,你真的没有什么可以做的。有一些库等有助于创建沙箱,但很难从自身沙箱对象。
Edit: Somehow I missed Artefacto's answer, and I suppose he is right (I've never tried writing an extension to do that). Still, implementing it breaks developer expectations while making it harder to actually see code to explain what's going in.
编辑:不知何故,我错过了 Artefacto 的答案,我想他是对的(我从未尝试过编写扩展来做到这一点)。尽管如此,实现它还是打破了开发人员的期望,同时让实际看到代码来解释正在发生的事情变得更加困难。

