Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I don't define a constructor for a subclass, can I use the constructor of the superclass directly?

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;
 }
like image 465
Uğur Gümüşhan Avatar asked Nov 21 '11 08:11

Uğur Gümüşhan


People also ask

Can a subclass use a superclass constructor?

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

What happens if a subclass does not have a constructor?

If the subclass has no constructor at all, the compiler will automatically create a public constructor that simply calls through to the default constructor of the superclass.

Is the superclass constructor defined in the subclass?

Subclass Constructors Invocation of a superclass constructor must be the first line in the subclass constructor.

Can a subclass constructor override a superclass constructor?

Constructors are never overriden or inherited. In fact, they cannot be called virtually. However, you can create a constructor in the subclass which calls a constructor in the parent class.


2 Answers

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;
 }
like image 176
Moe Sweet Avatar answered Oct 20 '22 10:10

Moe Sweet


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
}
like image 43
Utku Yıldırım Avatar answered Oct 20 '22 10:10

Utku Yıldırım