titreader Posted August 18, 2013 Share Posted August 18, 2013 In Chapter 5 on pate 157 it says in a darker portlet: ...when you create an object of a child class, the parent class's constructor is not automatically called... I need some more details on this. What concerns me is that I created a file in which I included Rectangle.php then I declared a new class such as: class Square extends Rectangle { } just an empty class and then called: $r = new Square(); for testing purposes I added the following line to Rectangle's constructor class: print "construction called"; and it does seem to be executed when new Square class is instantiated with no parameters passed! Is the documentation in the book wrong or am I missing something? Because the above code indicates that the parent constructor seems to be called? Thanks in advance Link to comment Share on other sites More sharing options...
titreader Posted August 18, 2013 Author Share Posted August 18, 2013 Further to the above I think that the statement in the book is incomplete. The parent constructor is not called only when such is present on the child's class side. But it should make it clear that if the child's class does not have a constructor declared the parent one is automatically called Link to comment Share on other sites More sharing options...
Larry Posted August 20, 2013 Share Posted August 20, 2013 Yes, right. The parent's constructor is not automatically called if the child has a constructor. Sorry about the confusion! Link to comment Share on other sites More sharing options...
Recommended Posts