Index
    Preface
      What This Book Is About
      What You Need to Know
      How This Book Is Organized
      How to Use This Book
      Conventions Used in This Book
      Using Code Examples
      How to Contact Us
      Web Site and Code Examples
      Acknowledgments
      Chapter 1.  Database Applications and the Web
      Section 1.1.  The Web
      Section 1.2.  Three-Tier Architectures
      Chapter 2.  The PHP Scripting Language
      Section 2.1.  Introducing PHP
      Section 2.2.  Conditions and Branches
      Section 2.3.  Loops
      Section 2.4.  Functions
      Section 2.5.  Working with Types
      Section 2.6.  User-Defined Functions
      Section 2.7.  A Working Example
      Chapter 3.  Arrays, Strings, and Advanced Data Manipulation in PHP
      Section 3.1.  Arrays
      Section 3.2.  Strings
      Section 3.3.  Regular Expressions
      Section 3.4.  Dates and Times
      Section 3.5.  Integers and Floats
      Chapter 4.  Introduction to Object-Oriented Programming with PHP 5
      Section 4.1.  Classes and Objects
      Section 4.2.  Inheritance
      Section 4.3.  Throwing and Catching Exceptions
      Chapter 5.  SQL and MySQL
      Section 5.1.  Database Basics
      Section 5.2.  MySQL Command Interpreter
      Section 5.3.  Managing Databases and Tables
      Section 5.4.  Inserting, Updating, and Deleting Data
      Section 5.5.  Querying with SQL SELECT
      Section 5.6.  Join Queries
      Section 5.7.  Case Study: Adding a New Wine
      Chapter 6.  Querying Web Databases
      Section 6.1.  Querying a MySQL Database Using PHP
      Section 6.2.  Processing User Input
      Section 6.3.  MySQL Function Reference
      Chapter 7.  PEAR
      Section 7.1.  Overview
      Section 7.2.  Core Components
      Section 7.3.  Packages
      Chapter 8.  Writing to Web Databases
      Section 8.1.  Database Inserts, Updates, and Deletes
      Section 8.2.  Issues in Writing Data to Databases
      Chapter 9.  Validation with PHP and JavaScript
      Section 9.1.  Validation and Error Reporting Principles
      Section 9.2.  Server-Side Validation with PHP
      Section 9.3.  JavaScript and Client-Side Validation
      Chapter 10.  Sessions
      Section 10.1.  Introducing Session Management
      Section 10.2.  PHP Session Management
      Section 10.3.  Case Study: Using Sessions in Validation
      Section 10.4.  When to Use Sessions
      Section 10.5.  PHP Session API and Configuration
      Chapter 11.  Authentication and Security
      Section 11.1.  HTTP Authentication
      Section 11.2.  HTTP Authentication with PHP
      Section 11.3.  Form-Based Authentication
      Section 11.4.  Protecting Data on the Web
      Chapter 12.  Errors, Debugging, and Deployment
      Section 12.1.  Errors
      Section 12.2.  Common Programming Errors
      Section 12.3.  Custom Error Handlers
      Chapter 13.  Reporting
      Section 13.1.  Creating a Report
      Section 13.2.  Producing PDF
      Section 13.3.  PDF-PHP Reference
      Chapter 14.  Advanced Features of Object-Oriented Programming in PHP 5
      Section 14.1.  Working with Class Hierarchies
      Section 14.2.  Class Type Hints
      Section 14.3.  Abstract Classes and Interfaces
      Section 14.4.  Freight Calculator Example
      Chapter 15.  Advanced SQL
      Section 15.1.  Exploring with SHOW
      Section 15.2.  Advanced Querying
      Section 15.3.  Manipulating Data and Databases
      Section 15.4.  Functions
      Section 15.5.  Automating Querying
      Section 15.6.  Table Types
      Section 15.7.  Backup and Recovery
      Section 15.8.  Managing Users and Privileges
      Section 15.9.  Tuning MySQL
      Chapter 16.  Hugh and Dave's Online Wines:A Case Study
      Section 16.1.  Functional and System Requirements
      Section 16.2.  Application Overview
      Section 16.3.  Common Components
      Chapter 17.  Managing Customers
      Section 17.1.  Code Overview
      Section 17.2.  Customer Validation
      Section 17.3.  The Customer Form
      Chapter 18.  The Shopping Cart
      Section 18.1.  Code Overview
      Section 18.2.  The Winestore Home Page
      Section 18.3.  The Shopping Cart Implementation
      Chapter 19.  Ordering and Shipping at the Online Winestore
      Section 19.1.  Code Overview
      Section 19.2.  Credit Card and Shipping Instructions
      Section 19.3.  Finalizing Orders
      Section 19.4.  HTML and Email Receipts
      Chapter 20.  Searching and Authentication in the Online Winestore
      Section 20.1.  Code Overview
      Section 20.2.  Searching and Browsing
      Section 20.3.  Authentication
      Appendix A.  Linux Installation Guide
      Section A.1.  Finding Out What's Installed
      Section A.2.  Installation Overview
      Section A.3.  Installing MySQL
      Section A.4.  Installing Apache
      Section A.5.  Installing PHP
      Section A.6.  What's Needed for This Book
      Appendix B.  Microsoft Windows Installation Guide
      Section B.1.  Installation Overview
      Section B.2.  Installing with EasyPHP
      Section B.3.  What's Needed for This Book
      Appendix C.  Mac OS X Installation Guide
      Section C.1.  Getting Started
      Section C.2.  Installing MySQL
      Section C.3.  Setting Up Apache and PHP
      Section C.4.  What's Needed for This Book
      Appendix D.  Web Protocols
      Section D.1.  Network Basics
      Section D.2.  Hypertext Transfer Protocol
      Appendix E.  Modeling and Designing Relational Databases
      Section E.1.  The Relational Model
      Section E.2.  Entity-Relationship Modeling
      Appendix F.  Managing Sessions in theDatabase Tier
      Section F.1.  Using a Database to Keep State
      Section F.2.  PHP Session Management
      Section F.3.  MySQL Session Store
      Appendix G.  Resources
      Section G.1.  Client Tier Resources
      Section G.2.  Middle-Tier Resources
      Section G.3.  Database Tier Resources
      Section G.4.  Security and Cryptography Resources
      Appendix H.  The Improved MySQL Library
      Section H.1.  New Features
      Section H.2.  Getting Started
      Section H.3.  Using the New Features
    Colophon
    Copyright



 

Previous Section  < Day Day Up >  Next Section

14.1 Working with Class Hierarchies

In Chapter 4, we showed you how inheritance can be used as a powerful tool in object-oriented programming. Using the extends keyword, a class can inherit the capabilities from a parent class. As we explained in Chapter 4, PHP allows an inheritance from only one parent class, but a class can be extended to create any number of child classes.

Inheritance allows functionality to be built up from related classes. Each class provides specialized support that's specific to a class's purpose. It's common that functionality in an object-oriented application is provided by class hierarchies rather than single, unrelated classes.

Example 14-1 shows how a class hierarchy is formed to provide functionality that describes shapes.

Example 14-1. Shape classes
<?php



class Shape

{

    var $color;

    var $sides;



    function color( ) 

    { 

        return $this->color; 

    }



    function sides( )

    {

        return $this->sides;

    }



    function _  _construct($color, $sides)

    {

        $this->color = $color;

        $this->sides = $sides;

    }

}



class Circle extends Shape

{



    function _  _construct($color)

    {

        parent::_  _construct($color, 1);

    }

}



class Polygon extends Shape

{

    var $angles;



    function angles( )

    {

        return $this->angles;

    }



    function _  _construct($color, $sides)

    {

         parent::_  _construct($color, $sides);

         $this->angles = $sides;

    }

}



class Triangle extends Polygon

{

    function _  _construct($color)

    {

        parent::_  _construct($color, 3);

    }

}



class Rectangle extends Polygon

{

    function _  _construct($color)

    {

        parent::_  _construct($color, 4);

    }

}



?>

The class Shape defined in the Example 14-1 supports only two features: the color and number of sides of a shape. The Circle and Polygon classes both extend the base Shape class: the Circle constructor function always sets the number of sides to 1,[1] while the Polygon class allows an arbitrary number of sides and defines the member function angles( ) that returns the number of angles the shape makes.

[1] The number of sides of a circle depends on your definition of side—some definitions lead to no sides, some to an infinite number. By our definition, a circle has one side.

The Polygon class is extended by the Triangle and Rectangle classes, and each defines a constructor function that sets the appropriate number of sides. The relationship between the classes defined in Example 14-1 is shown in the class diagram in Figure 14-1.

Figure 14-1. Class hierarchy for the Shape classes
figs/wda2_1401.gif


The classes defined in Example 14-1 aren't particularly useful—we kept the functionality to a minimum to illustrate how a hierarchy is formed. A real application that deals with shapes would define member variables and functions to support the application requirements. For example, if you were interested in the size of a shape, you could add member variables to record the dimensions, and functions that calculate the area.

14.1.1 Polymorphism

While a class hierarchy can help you to develop modular code, most of the power of object-oriented programming comes from the ability to use an object differently in different circumstances, a capability known as polymorphism . Consider the objects that we can create from the classes defined in Example 14-1. The following fragment creates an array of objects using the Circle, Triangle, and Rectangle classes; and then prints information about each shape using a foreach loop.

// Create an array of objects

$shapes = array(

    new Triangle("blue"),

    new Circle("green"),

    new Rectangle("red"));



foreach ($shapes as $s)

    print "I have a {$s->color( )} shape with {$s->sides( )} sides\n";

We can call the color( ) and sides( ) member functions on each of the objects in the previous example because each object, through inheritance, is a Shape. We can't call the angles( ) function on each object because not all of the objects are Polygons and only instances of Polygons have the angles( ) member function. The previous example prints:

I have a blue shape with 3 sides

I have a green shape with 1 sides

I have a red shape with 4 sides

We give further examples that show the benefits of polymorphic behavior later in this chapter.

14.1.2 Discovering Relationships

The instanceof keyword is available in PHP5, and the is_a( ), get_class( ), and get_parent_class( ) functions are available in PHP4 and PHP5.

When you're dealing with an object in a PHP script, it's not always obvious what type of object it is. For example, while all the objects we created in the previous example are Shapes, only the Triangle and Rectangle objects can be used as Polygon objects. PHP5 supports the instanceof operator that allows you to write scripts that can test the capabilities of an object before using it. The following fragment shows how each shape object is tested before an attempt is made to call the Polygon member function:

// Create an array of objects

$shapes = array(

    new Triangle("blue"),

    new Circle("green"),

    new Rectangle("red"));



foreach ($shapes as $s)

{

    if ($s instanceof Polygon)

        print "I have a {$s->color( )} polygon with {$s->sides( )} 

              sides and {$s->angles( )} internal angles\n";

    else

        print "I have a {$s->color( )} shape with {$s->sides( )} sides\n";

}

The previous example prints the longer Polygon message for the Triangle and Rectangle objects, and the shorter Shape message for the Circle object:.

I have a blue polygon with 3 

              sides and 3 internal angles

I have a green shape with 1 sides

I have a red polygon with 4 

              sides and 4 internal angles

14.1.2.1 Functions

The instanceof keyword performs a similar function to the PHP library function is_a( ). Both evaluate to true if the object is an instance of the test class, or an ancestor class. Here's an example that uses is_a( ) to perform the same function as in the previous example:

    if (is_a($s "Polygon"))

        print "I have a {$s->color( )} polygon with {$s->sides( )} 

              sides and {$s->angles( )} internal angles\n";

    else

        print "I have a {$s->color( )} shape with {$s->sides( )} sides\n";

PHP provides several related functions that return information about the class hierarchy of an object:

boolean is_subclass_of(object obj, string classname)
string get_class(object obj)
string get_parent_class(object obj)

The function is_subclass_of( ) returns true if the class of object obj is a descendant of classname. The get_class( ) function returns the name of the class for the object obj, and get_parent_class( ) returns the parent class name. Both get_class( ) and get_parent_class( ) normalize the name of the class to lower case as demonstrated in the following fragment:

// Create a new Triangle object

$shape = new Triangle("orange");



// prints "triangle"

print get_class($shape);



// prints "polygon"

print get_parent_class($shape);

    Previous Section  < Day Day Up >  Next Section







    Copyright © 2010 | Domen maybe sale - bye this domen