Jump to content

I Can Not Understand This Parent ::


zetcoby

Recommended Posts

okay i have a problem understanding things like this one

parent::__construct();

i would appreciate if someone can explain me what the ' :: ' means i know what a construct is, the strange part for me is that i found that many times in a other construct something like this:

function __construct()
{
parent::__construct();
}

Link to comment
Share on other sites

:: has nothing to do with namespacing, that stack overflow article is talking about an entirely different language.

 

parent:: is a special construct in PHP that refers to the class which the current class extends. parent::foo() calls the function foo on the parent, and parent::__construct() calls the parent's constructor. Normally the :: operator indicates that a function is being called statically, but for parent:: it can be static or dynamic.

 

The usage for parent::__construct() inside the child constructor is to ensure that the object you get back is the child, but the code for the constructor in the parent is still run. It's not actually necessary to have a child constructor with nothing but parent::__construct() in it, but some people like to be explicit, and usually there's at least one other activity in the child constructor before the parent's is called.

Link to comment
Share on other sites

Much of this post is basically what ManiacDan wrote, because he summarized the concepts pretty well.

 

It is simply used for accessing the constructor of the parent (the class that the child extends). It could also call any other method, e.g. parent::getSomething(). To better understand the principles of inheritance, I suggest that you do a Google search for this term.

 

Regarding the __construct() method, this is a class' constructor. This method will be run when a class instance is initialized, e.g. with the following code:

 

$user = new User();

 

You can them perform initialization within this method. In languages such as Java or C#, you would often initialize an ArrayList or List in the constructor, for instance. In these languages, the constructor is a method with the same name as the class itself, which differs from PHP. However, the principle is the same. There is also a __destruct() method which is executed when an object is destroyed, e.g. by doing like this:

 

unset($user);

 

You can read more about constructors and destructors here.

 

As ManiacDan stated, it is not always necessary to define a constructor in the child's class:

 

Note: 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.

 

What this means is that if the child class has a constructor and you still want to call the parent's constructor, then you will have to call it with parent::construct(). Otherwise it is done automatically (that is, if you do not define a constructor for the child class). As ManiacDan stated, you will usually put other instructions in the child constructor if you define one.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.