11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

www.it-ebooks.infoCHAPTER 7 • ADVANCED OOP FEATURESMy name is Richard.I'm selling company assets to finance my yacht!Because all employees have a name, the Executive class inherits from the Employee class, saving youthe hassle of having to re-create the name property and the corresponding getter and setter. You can thenfocus solely on those characteristics that are specific to an executive, in this case a method namedpillageCompany(). This method is available solely to objects of type Executive, and not to the Employeeclass or any other class—unless you create a class that inherits from Executive. The following exampledemonstrates that concept, producing a class titled CEO, which inherits from Executive:class Employee {...}class Executive extends Employee {...}class CEO extends Executive {function getFacelift() {echo "nip nip tuck tuck";}}$ceo = new CEO();$ceo->setName("Bernie");$ceo->pillageCompany();$ceo->getFacelift();Because Executive has inherited from Employee, objects of type CEO have all the properties andmethods that are available to Executive in addition to the getFacelift() method, which is reservedsolely for objects of type CEO.Inheritance and ConstructorsA common question pertinent to class inheritance has to do with the use of constructors. Does a parentclass constructor execute when a child is instantiated? If so, what happens if the child class also has itsown 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, providedthat the child class does not also have a constructor. For example, suppose that the Employee class offersthis constructor:165

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!