Programming Basics

  Home arrow Programming Basics arrow Page 5 - Beginning Object Oriented Programming ...
PROGRAMMING BASICS

Beginning Object Oriented Programming in PHP
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 241
    2003-11-11

    Table of Contents:
  • Beginning Object Oriented Programming in PHP
  • Objects - The Magic Box
  • Classes
  • Methods
  • Extends
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Beginning Object Oriented Programming in PHP - Extends


    (Page 5 of 6 )

    So far our example is only capable of telling us what its content is. We could add more methods to our class, but what if we were building extensions to someone else's class? We don't have to create an entirely new class to add new functionality... we can build a small extension class based on the original.

    <?php
    ;
    include(
    "class.Box.php");

    class 
    ShoeBox extends Box
    {
      
    varr $size;

      function 
    ShoeBox($contents$size) {
        
    $this-&gt;contents $contents;
        
    $this-&gt;size $size;
      }

      function 
    get_shoe_size() {
        return 
    $this-&gt;size;
      }
    }

    $mybox = new ShoeBox("Shoes"10);
    echo 
    $mybox-&gt;get_whats_inside();
    echo 
    $mybox-&gt;get_shoe_size();
    ?>

    With the extends keyword, our script now has access to a ShoeBox class which is based on our original Box class. ShoeBox has all of the same functionality as Box class but also has extra functions specific to a special kind of box.

    The ability to write such modular additions to your code gives great flexibility in testing out new methods and saves time by reusing the same core code.

    <?php
    include("class.Box.php");
    include(
    "extentions.Shoe.php");
    include(
    "extentions.Suggestion.php");
    include(
    "extentions.Cardboard.php");

    $mybox = new ShoeBox("Shoes"10);
    $mySuggestion = new SuggestionBox("Complaints");
    $myCardboard = new CardboardBox(''"corrugated""18in""12in""10in");
    ?>

    More Programming Basics Articles
    More By bluephoenix

    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP
    - Calendar Construction with PHP
    - PHP`s Calendar Package
    - Getting Modified Versions and Correct Dates ...
    - Combining Date Functions in PHP
    - Using PHP for Date and Time in Programming
    - More Exception Handling with PHP
    - Exception Handling in PHP
    - Error Logging and Handling Exceptions
    - Configuration Directives for Error and Excep...
    - Error and Exception Handling
    - Python Modules for Games


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