Miscellaneous

  Home arrow Miscellaneous arrow Inheritance and Other Advanced OOP Fea...
MISCELLANEOUS

Inheritance and Other Advanced OOP Features
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2010-06-02

    Table of Contents:
  • Inheritance and Other Advanced OOP Features
  • Interfaces
  • Implementing a Single Interface
  • Abstract Classes

  •  
     

    SEARCH CODEWALKERS

    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) {
       $this->setName($name);
    }

    Then you instantiate theCEO class and retrieve thenamemember:

    $ceo = new CEO("Dennis");
    echo $ceo->getName();

    It will yield the following:

    --------------------------------------------
    My name is Dennis
    --------------------------------------------

    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() {
         echo "<p>CEO 
    object created!</p>";
     
    }

    Then you instantiate theCEOclass:

    $ceo = new CEO("Dennis");
    echo $ceo->getName();

    This time it will yield the following, because theCEO constructor overrides theEmployeeconstructor:

    --------------------------------------------
    CEO object created!
    My name is
    --------------------------------------------

    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) {
         parent::__construct($name);
         echo "
    <p>CEO object created!</p>";
      } 

    Again instantiating theCEO class and executinggetName()in the same fashion as before, this time youíll see a different outcome:

    --------------------------------------------
    CEO object created!
    My name is Dennis
    --------------------------------------------

    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) {
        
    Employee::__construct($name);
        
    Executive::__construct();
        
    echo " <p>CEO object created!</p>";
     
    }

    More Miscellaneous Articles
    More By Apress Publishing

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Attention: Forum and Site Maintenance
    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength

    Developer Shed Affiliates

     



    © 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap