PHP Codeigniter - parent::__construct

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17809517/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:30:29  来源:igfitidea点击:

PHP Codeigniter - parent::__construct

phpcodeigniter

提问by Seong Lee

When getting inherited from a parent class in PHP, especially in Codeigniter what does parent::__construct or parent::model()do?

当从 PHP 中的父类继承时,尤其是在 Codeigniter 中,它会parent::__construct or parent::model()做什么?

How would it make difference if I don't __constructparent class? And, which way is suggested?

如果我不上__construct家长课,会有什么不同?而且,建议采用哪种方式?

-Added-

-添加-

The focus is more on Codeigniter specific regarding a call to parent::__constructin different ways depending on versions and also if this could be omitted in case Codeigniter would do this automatically.

重点更多地放在 Codeigniter 特定parent::__construct于根据版本以不同方式调用,以及如果 Codeigniter 会自动执行此操作时是否可以省略。

回答by giorgio

This is a normal class constructor. Let's look at the following example:

这是一个普通的类构造函数。让我们看看下面的例子:

class A {
    protected $some_var;

    function __construct() {
        $this->some_var = 'value added in class A';
    }

    function echo_some_var() {
        echo $this->some_var;
    }
}

class B extends A {
    function __construct() {
        $this->some_var = 'value added in class B';
    }
}

$a = new A;
$a->echo_some_var(); // will print out 'value added in class A'
$b = new B;
$b->echo_some_var(); // will print out 'value added in class B'

As you see, class B inherits all values and functions from A. So the class member $some_varis accessible from A as well as from B. Because we've added a constructor in class B, the constructor of class A will NOT be used when you are creating a new object of class B.

如您所见,类 B 继承了 A 的所有值和函数。因此类成员$some_var可以从 A 和 B 访问。因为我们在类 B 中添加了一个构造函数,所以当您使用类 A 的构造函数时,将不会使用它正在创建一个 B 类的新对象。

Now look at the following examples:

现在看看下面的例子:

class C extends A {
    // empty
}
$c = new C;
$c->echo_some_var(); // will print out 'value added in class A'

As you can see, because we have not declared a constructor, the constructor of class A is used implicitly. But we can also do the following, which is equivalent to class C:

可以看到,因为我们没有声明构造函数,所以隐式使用了类A的构造函数。但是我们也可以做下面的,相当于C类:

class D extends A {
    function __construct() {
        parent::__construct();
    }
}
$d = new D;
$d->echo_some_var(); // will print out 'value added in class A'

So you only have to use the line parent::__construct();when you want a constructor in the child class to do something, AND execute the parent constructor. Example given:

因此,parent::__construct();当您希望子类中的构造函数执行某些操作并执行父构造函数时,您只需使用该行。给出的例子:

class E extends A {
    private $some_other_var;

    function __construct() {
        // first do something important
        $this->some_other_var = 'some other value';

        // then execute the parent constructor anyway
        parent::__construct();
    }
}

More information can be found here: http://php.net/manual/en/language.oop5.php

更多信息可以在这里找到:http: //php.net/manual/en/language.oop5.php

回答by user3274600

what does parent::__construct or parent::model() do?

parent::__construct 或 parent::model() 做什么?

these functions do exactly the same, only the construct function used to be named after the class itself prior to PHP5. I say in your example you are extending the Model class (and on some older version of CI since you don't need to use CI_model), if I'm correct in this __construct is the same as model().

这些函数的作用完全相同,只是构造函数在 PHP5 之前以类本身命名。我在您的示例中说您正在扩展 Model 类(并且在某些旧版本的 CI 上,因为您不需要使用 CI_model),如果我在这个 __construct 中是正确的,则与 model() 相同。

回答by Justin Herrera

It will simply execute the constructor of parent class

它将简单地执行父类的构造函数