如果子类没有定义构造函数,可以直接使用超类的构造函数吗?

发布于 2024-12-17 16:18:59 字数 384 浏览 0 评论 0原文

构造函数是继承的还是属于定义它们的类?我只见过子类构造函数调用超类构造函数的示例。这是我当前的代码,它可以给出一些关于正在发生的事情的提示。 (我会根据你的回复更改代码。如果我可以使用超类的构造函数,我就不会为每个子类定义一个构造函数并从每个子类中调用超类的构造函数。

abstract class view
{
 public $vieverid;

 function __construct($viewerid) {
  $this->viewer = $viewerid;
 }
}
class viewactor extends view{

 function __construct($viewerid) {
  $this->viewerid = $viewerid;
 }

Are constructors inherited or do they belong to the class they are defined in? I only have seen examples with constructors of subclasses which call superclass' constructors. This is my current code, which can give some hint about what's going on. (I will change the code according to your replies. If I can use the constructor of the superclass, I won't define a constructor for each subclass and call superclass' constructor from each.

abstract class view
{
 public $vieverid;

 function __construct($viewerid) {
  $this->viewer = $viewerid;
 }
}
class viewactor extends view{

 function __construct($viewerid) {
  $this->viewerid = $viewerid;
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

友谊不毕业 2024-12-24 16:18:59

根据我的理解,如果定义了子构造函数,PHP不会自动调用父构造函数。否则就是这样。

在子构造函数中,您必须手动调用父构造函数。

abstract class view
{
 public $vieverid;

 function __construct($viewerid) {
  $this->viewer = $viewerid;
 }
}
class viewactor extends view{

 function __construct($viewerid) {
  parent::__construct($viewerid); // manual call
  // do your stuff here...
  $this->viewerid = $viewerid;
 }

According to my understanding, PHP doesn't auto-call parent's constructor if child constructor is defined. Otherwise it does.

In child constructor you have to call parent's constructor manually.

abstract class view
{
 public $vieverid;

 function __construct($viewerid) {
  $this->viewer = $viewerid;
 }
}
class viewactor extends view{

 function __construct($viewerid) {
  parent::__construct($viewerid); // manual call
  // do your stuff here...
  $this->viewerid = $viewerid;
 }
转瞬即逝 2024-12-24 16:18:59

parent::__construct(params); 用于调用超类构造函数

PHP4

PHP 不会自动从
派生类的构造函数。宣传是你的责任
在适当的情况下调用上游构造函数。

PHP5

如果定义了新的构造函数,PHP 不会调用基类的构造函数。
如果为派生类定义构造函数
宣传是你的责任
在适当的情况下调用上游构造函数。
父级::__construct(参数)

构造函数

abstract class view
{
 public $vieverid;

 function __construct($viewerid) {
  $this->vieverid= $viewerid;
 }
}

class viewactor extends view{

 function __construct($viewerid) {
   parent::__construct($viewerid);
   // Extra code if you want
 }
}

class viewactor_construct extends view{
    // Works in PHP5
}

parent::__construct(params); use for calling superclass constructor

PHP4

PHP doesn't call constructors of the base class automatically from a
constructor of a derived class. It is your responsibility to propagate
the call to constructors upstream where appropriate.

PHP5

PHP doesn't call constructors of the base class if new constructor defined.
If you define a constructor for derived class
It is your responsibility to propagate
the call to constructors upstream where appropriate.
parent::__construct(params)

Constructors

abstract class view
{
 public $vieverid;

 function __construct($viewerid) {
  $this->vieverid= $viewerid;
 }
}

class viewactor extends view{

 function __construct($viewerid) {
   parent::__construct($viewerid);
   // Extra code if you want
 }
}

class viewactor_construct extends view{
    // Works in PHP5
}
走过海棠暮 2024-12-24 16:18:59

如果子类定义了构造函数,则不会隐式调用父构造函数。为了运行父构造函数,需要在子构造函数中调用parent::__construct()。

参见此处

Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.

See here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文