zetcoby Posted October 19, 2012 Share Posted October 19, 2012 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(); } Quote Link to comment https://forums.phpfreaks.com/topic/269666-i-can-not-understand-this-parent/ Share on other sites More sharing options...
JohnTipperton Posted October 19, 2012 Share Posted October 19, 2012 (edited) :: it is an operator to define namespace for further details visit this link --> http://stackoverflow.com/questions/4269034/what-is-the-meaning-of-prepended-double-colon-to-class-name Edited October 19, 2012 by JohnTipperton Quote Link to comment https://forums.phpfreaks.com/topic/269666-i-can-not-understand-this-parent/#findComment-1386257 Share on other sites More sharing options...
ManiacDan Posted October 19, 2012 Share Posted October 19, 2012 :: 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. Quote Link to comment https://forums.phpfreaks.com/topic/269666-i-can-not-understand-this-parent/#findComment-1386296 Share on other sites More sharing options...
Andy123 Posted October 19, 2012 Share Posted October 19, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/269666-i-can-not-understand-this-parent/#findComment-1386393 Share on other sites More sharing options...
zetcoby Posted October 22, 2012 Author Share Posted October 22, 2012 thank you guys, it helped me a lot , now i understand it, finally ^^ Quote Link to comment https://forums.phpfreaks.com/topic/269666-i-can-not-understand-this-parent/#findComment-1386888 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.