简体   繁体   English

在PHP中的子构造函数之前调用父构造函数

[英]Call parent constructor before child constructor in PHP

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. 我想这样做的原因是能够加载一组特定于Form的标签可以在调用__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 Tag.php

class Tag {
     private $privateInfo;
     public $publicInfo;
     __construct($private, $public) {
         $this->privateInfo = $private;
         $this->publicInfo = $public;
     }
} 

Form.php form.php的

class Form extends Tag {

     __construct() {
         echo $this->publicInfo;
     }
} 

Make sense? 说得通?

Thanks! 谢谢! Matt Mueller 马特穆勒

Just call parent::__construct in the child. 只需在子节点中调用parent :: __ construct即可。

class Form extends Tag
{
    function __construct()
    {
        parent::__construct();
        // Called second.
    }
}

是的只是在你的构造中调用parent::__construct()

Yes, but only internally (ie, by writing a PHP extension), so if I were you I'd settle with calling parent::__construct() . 是的,但只是在内部(即通过编写PHP扩展),所以如果我是你,我会选择调用parent::__construct() See this section on the PHP wiki. 请参阅PHP wiki上的此部分

Sorry, PHP is not Java. 对不起,PHP不是Java。 I think not requiring (implicitly or explictly) the super constructor to be called was a very poor design decision. 我认为不需要(隐式地或明确地)要调用的超级构造函数是一个非常糟糕的设计决策。

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. 这样每个人都知道你不希望这被覆盖,任何继承的类(正确地)都有权在构造函数中做任何想做的事情。

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. 另一个,不推荐,更“重新发明轮子”,解决方案是在父类中定义一个函数,比如_construct(),它在自己的构造中调用。 Its not really clear, doesn't use language features/constructs, and is very specific to a single application. 它不是很清楚,不使用语言特性/结构,并且非常特定于单个应用程序。

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. 使用Reflection,serialize,var_dump,var_export以及php语言中的所有其他方便的API,如果有代码不应该对数据做任何事情,那么除了不存储它之外你不能做太多的事情。 There are libraries and such that help create sandboxes, but its hard to sandbox an object from itself. 有一些库可以帮助创建沙箱,但是它很难将一个对象自己沙箱化。

Edit: Somehow I missed Artefacto's answer, and I suppose he is right (I've never tried writing an extension to do that). 编辑:不知怎的,我错过了Artefacto的答案,我想他是对的(我从来没有尝试过编写扩展来做到这一点)。 Still, implementing it breaks developer expectations while making it harder to actually see code to explain what's going in. 尽管如此,实施它仍会打破开发人员的期望,同时更难以实际看到代码来解释发生了什么。

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

相关问题 是否必须从PHP中的子类的构造函数中调用parent :: __构造? - Is it mandatory to call parent::__construct from the constructor of child class in PHP? PHP:从父方法中的静态方法调用子构造函数 - PHP: call child constructor from static method in parent 如何在PHP的所有子实例中仅一次调用父构造函数 - how to call parent constructor just once in all child instances in php Laravel 4 - 子构造函数通过依赖注入调用父构造函数 - Laravel 4 - Child constructor call parent constructor with dependency injection PHP-如果我们在php中创建子类的对象,它会调用父类构造函数,然后调用子类构造函数(如java)吗? - PHP -If we create object of child class in php, does it call parent class constructor and then child class constructor like java? 子类构造函数将如何与php中的父类构造函数交互? - How will a child class constructor interact with a parent class constructor in php? 在子PHP之后自动调用父构造函数 - Call parent constructor automatically after children PHP 在子构造函数中调用父构造函数 - calling parent constructor inside child constructor laravel父构造函数不称为子构造函数 - laravel parent constructor does not called child constructor 从子类调用父类成员而不调用构造函数依赖项 - Call to a parent class member from child without calling constructor dependencies
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM