-3

If I create a class and define a public constructor of it and I also create a child class of the parent class, it also have constructor.

Then how can I call these two constructors from one of the method of subclass? I mean how to call two or more constructor from one method of a child class in php?

3 Answers3

1

In C++:

You can call just by creating a child object.

When you just create a child object it first calls Parent Constructor and then The child Constructor.

Example:

Class Parent {
    void Parent :: Parent() {
        cout << "I am parent Constructor!" << endl;
    }
};
Class Child : Public Parent() {
    void Child :: Child() {
        cout << "I am Child Constructor" << endl;
    }
};
int main() {
    Child childobj;
}

Output:

"I am parent Constructor!"
"I am Child Constructor"

For PHP

class Parent {
    public function __construct($bypass = false) {
        // Only perform actions inside if not bypassing.
        if (!$bypass) {

        }
    }
}
class Child extends Parent {
    public function __construct() {
        $bypassPapa = true;
        parent::__construct($bypassPapa);
    }
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
learncode
  • 1,105
  • 4
  • 18
  • 36
  • Thank you. I am asking about OOP. – Shamima Rashid Sumi Dec 30 '16 at 20:42
  • 1
    @ShamimaRashidSumi Yeah Which Language that has OOP? – learncode Dec 30 '16 at 20:42
  • @ShamimaRashidSumi C++ is the Object-Oriented version of the C language. The example above uses constructor Overriding which is a principal of OOP. – Mr. Polywhirl Dec 30 '16 at 20:44
  • @ShamimaRashidSumi OOp is just a programming language model, it can be exhibited my many languages like Java, C++, Python.. Which language are you asking for specifically? – learncode Dec 30 '16 at 20:45
  • I am talking about php. – Shamima Rashid Sumi Dec 30 '16 at 20:54
  • @ShamimaRashidSumi I have updated my code have a look!, By posting a language you can help us better understand the question. – learncode Dec 30 '16 at 21:01
  • Note that you [cannot OVERLOAD a constructor in PHP](http://stackoverflow.com/questions/2169448/why-cant-i-overload-constructors-in-php) and you must handle if-else logic within the constructor for it to [simulate alternative construction logic](http://stackoverflow.com/questions/8210561/best-practice-overriding-construct-versus-providing-init-method); per instance. – Mr. Polywhirl Dec 30 '16 at 21:03
  • Why p here parent::__construct($bypassPapa); is in small caps ? Why not Parent::__construct($bypassPapa); ? – flash Feb 27 '23 at 18:39
0

I am answering this based specifically on C++ programming, as I am not certain which OOP language you are using, but I expect that the principles, if not the specific syntax, will apply.

When you define a class with at least one constructor, the compiler will not generate an implicit constructor. As such, if the constructor(s) you define for the base class require parameters, they must be included in a specific call from the constructor in the child class since there will be no parameter free constructor to call.

class Parent
    {
    public:
    Parent(int a,int a)
        :a(a),
        b(b)
    {
        cout<<"Parent constructor "<<a<<b;
    }
    ~Parent()
    {}
private:
    int a;
    int b;
};

class Child : public Parent
{
public:
    Child()
        :c(5) //error: implicit constructor for Parent is not found
    {
        cout<<"Child constructor "<<c;
    }
    ~Child()
    {}
private:
    int c;
};

int main()
{
    Child x;
    return 0;
}

This problem can be corrected by including a call to the Parent constructor within the constructor for the Child class as follows:

.
.
.
Child()
    :Parent(3,4),  // Explicit call to Parent constructor
    c(5)
    {
        cout<<"Child constructor "<<c;
    }
.
.
.

Hope this helps.

-1

In C#; When you create an instance of subclass, if the base class has a parameter-less constructor, It will be called. But if the base class has parameter constructor, you may call the constructor of that by following syntax.

Class SubClass : BaseClass(...)
{
   ...
}

In order to call constructor in other methods you need to have a protected method which was called by constructor, then you can call it from another method. Please note that you cannot call constructor from another method because it's a mechanism for instantiating (It should be called when an instance of that type is created)

Siyavash Hamdi
  • 2,764
  • 2
  • 21
  • 32