Miscellaneous

  Home arrow Miscellaneous arrow Page 4 - Advanced OOP Features
MISCELLANEOUS

Advanced OOP Features
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2010-05-26

    Table of Contents:
  • Advanced OOP Features
  • Object Cloning
  • Inheritance
  • Class Inheritance

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Advanced OOP Features - Class Inheritance


    (Page 4 of 4 )

    As applied to PHP, class inheritance is accomplished by using the extends keyword. Listing 7-3 demonstrates this ability, first creating anEmployeeclass, and then creating anExecutive class that inherits fromEmployee.


    ¦Note  A class that inherits from another class is known as a child class, or a subclass. The class from which the child class inherits is known as the parent, or base class.


    Listing 7-3. Inheriting from a Base Class

    <?php
      
    # Define a base Employee class
       class Employee {

          private $name;

          # Define a setter for the private $name member.
         
    function setName($name) {
             if ($name == "") echo "Name cannot be blank!";
             else $this->name = $name;
         
    }

          # Define a getter for the private $name member
          function getName() {
             return "My name is ".$this->name."<br />";
          }
       } #end Employee class

       # Define an Executive class that inherits from Employee
      
    class Executive extends Employee {
          # Define a method unique to Employee
          function pillageCompany() {
            
    echo "I'm selling company assets to finance my yacht!";
          }
       } #end Executive class

       # Create a new Executive object
       $exec = new Executive();

       # Call the setName() method, defined in the Employee class
       $exec->setName("Richard");

       # Call the getName() method
       echo $exec->getName();

       # Call the pillageCompany() method
       $exec->pillageCompany();
    ?>

    This returns the following:

    --------------------------------------------
    My name is Richard.
    I'm selling company assets to finance my yacht!
    --------------------------------------------

    Because all employees have a name, theExecutiveclass inherits from theEmployeeclass, saving you the hassle of having to re-create thename member and the corresponding getter and setter. You can then focus solely on those characteristics that are specific to an executive, in this case a method namedpillageCompany(). This method is available solely to objects of typeExecutive, and not to theEmployeeclass or any other class, unless of course we create a class that inherits fromExecutive. The following example demonstrates that concept, producing a class titledCEO, which inherits fromExecutive:

    <?php 

       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();

    ?>

    BecauseExecutivehas inherited fromEmployee, objects of typeCEOalso have all the members and methods that are available toExecutive.

    Please check back next week for the second part of this series.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.
    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - 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
    - Install Slackware on Your Old PC


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