| | |||||||
| |||||||
| |||||||
|
|
|
|
|
|
|
Inheritance and Other Advanced OOP Features(Page 1 of 4 ) In this second part of a three-part series on the advanced object-oriented programming features in PHP 5, you'll learn about inheritance, constructors, and more. This article is excerpted from chapter 7 of the book Beginning PHP and PostgreSQL 8: From Novice to Professional, written by W. Jason Gilmore and Robert H. Treat (Apress; ISBN: 1590595475). Inheritance and Constructors A common question pertinent to class inheritance has to do with the use of constructors. Does a parent class constructor execute when a child is instantiated? If so, what happens if the child class also has its own constructor? Does it execute in addition to the parent constructor, or does it override the parent? Such questions are answered in this section. If a parent class offers a constructor, it does execute when the child class is instantiated, provided that the child class does not also have a constructor. For example, suppose that theEmployeeclass offers this constructor: function __construct($name) { Then you instantiate theCEO class and retrieve thenamemember: $ceo = new CEO("Dennis"); It will yield the following: -------------------------------------------- However, if the child class also has a constructor, that constructor will execute when the child class is instantiated, regardless of whether the parent class also has a constructor. For example, suppose that in addition to theEmployee class containing the previously described constructor, theCEOclass contains this constructor: function __construct() { Then you instantiate theCEOclass: $ceo = new CEO("Dennis"); This time it will yield the following, because theCEO constructor overrides theEmployeeconstructor: -------------------------------------------- When it comes time to retrieve thenamemember, you find that it’s blank, because thesetName()method, which executes in theEmployeeconstructor, never fires. Of course, you’re quite likely going to want those parent constructors to also fire. Not to fear, because there is a simple solution. Modify theCEOconstructor like so: function __construct($name) { Again instantiating theCEO class and executinggetName()in the same fashion as before, this time you’ll see a different outcome: -------------------------------------------- You should understand that whenparent::__construct()was encountered, PHP began a search upward through the parent classes for an appropriate constructor. Because it did not find one inExecutive, it continued the search up to theEmployeeclass, at which point it located an appropriate constructor. If PHP had located a constructor in theEmployee class, then it would have fired. If you want both theEmployeeandExecutiveconstructors to fire, then you need to place a call toparent::__construct()in theExecutiveconstructor. You also have the option to reference parent constructors in another fashion. For example, suppose that both theEmployeeandExecutive constructors should execute when a newCEO object is created. As mentioned in the last chapter, these constructors can be referenced explicitly within theCEOconstructor like so: function __construct($name) { More Miscellaneous Articles |
| |
| |