You are previewing PHP Object-Oriented Solutions.
O'Reilly logo
PHP Object-Oriented Solutions

Book Description

With the surge of popularity of PHP 5, and with PHP 6 just around the corner, Object-Oriented Programming is now an important consideration for PHP developers. This version-neutral book is a gentle introduction to Object-Oriented Programming (OOP) that won't overburden you with complex theory. It teaches you the essential basics of OOP that you'll need to know before moving onto a more advanced level, and includes a series of pre-packaged scripts that you can incorporate into your existing sites with the minimum of effort.

It shows how OOP can be used to create reusable and portable code by walking you through a series of simple projects. The projects feature the sorts of things developers run up against every day, and include a validator for filtering user input, a simple Date class that avoids the need to remember all the esoteric format codes in PHP, and an XML generator.

  • Teaches the fundamentals of OOP

  • Simple projects show how OOP concepts work in the real world

  • Pre-packaged scripts can easily be added to your own projects

  • Table of Contents

    1. Copyright
    2. ABOUT THE AUTHOR
    3. ABOUT THE TECHNICAL REVIEWER
    4. ACKNOWLEDGMENTS
    5. INTRODUCTION
    6. 1. WHY OBJECT-ORIENTED PHP?
      1. 1.1. Understanding basic OOP concepts
        1. 1.1.1. How OOP evolved
        2. 1.1.2. Using classes and objects
        3. 1.1.3. Protecting data integrity with encapsulation
        4. 1.1.4. Polymorphism is the name of the game
        5. 1.1.5. Extending classes through inheritance
          1. 1.1.5.1. Deciding on a class hierarchy
        6. 1.1.6. Using best practice
      2. 1.2. How OOP has evolved in PHP
        1. 1.2.1. OOP since PHP 5
        2. 1.2.2. Preparing for PHP 6
      3. 1.3. Choosing the right tools to work with PHP classes
        1. 1.3.1. Using a specialized script editor
      4. 1.4. Chapter review
    7. 2. WRITING PHP CLASSES
      1. 2.1. Formatting code for readability
        1. 2.1.1. Using the Zend Framework PHP Coding Standard
        2. 2.1.2. Choosing descriptive names for clarity
      2. 2.2. Creating classes and objects
        1. 2.2.1. Defining a class
        2. 2.2.2. Controlling access to properties and methods
          1. 2.2.2.1. Quick review
        3. 2.2.3. Setting default values with a constructor method
      3. 2.3. Using inheritance to extend a class
        1. 2.3.1. Defining a child class
        2. 2.3.2. Accessing a parent class's methods and properties
          1. 2.3.2.1. Using the scope resolution operator
        3. 2.3.3. Controlling changes to methods and properties
          1. 2.3.3.1. Preventing a class or method from being overridden
          2. 2.3.3.2. Using class constants for properties
          3. 2.3.3.3. Creating static properties and methods
          4. 2.3.3.4. Quick review
      4. 2.4. Loading classes automatically
      5. 2.5. Exploring advanced OOP features
        1. 2.5.1. Creating abstract classes and methods
        2. 2.5.2. Simulating multiple inheritance with interfaces
        3. 2.5.3. Understanding which class an object is an instance of
          1. 2.5.3.1. Restricting acceptable data with type hinting
      6. 2.6. Using magic methods
        1. 2.6.1. Converting an object to a string
        2. 2.6.2. Cloning an object
        3. 2.6.3. Accessing properties automatically
        4. 2.6.4. Accessing methods automatically
        5. 2.6.5. Cleaning up with a destructor method
      7. 2.7. Handling errors with exceptions
        1. 2.7.1. Throwing an exception
        2. 2.7.2. Catching an exception
        3. 2.7.3. Extracting information from an exception
        4. 2.7.4. Extending the Exception class
      8. 2.8. Using comments to generate code hints
        1. 2.8.1. Writing PHPDoc comments
      9. 2.9. Chapter review
    8. 3. TAKING THE PAIN OUT OF WORKING WITH DATES
      1. 3.1. Designing the class
        1. 3.1.1. Examining the built-in date-related classes
          1. 3.1.1.1. Using the DateTime class
          2. 3.1.1.2. Setting the default time zone in PHP
          3. 3.1.1.3. Examining the DateTimeZone class
          4. 3.1.1.4. Using the DateTimeZone class
        2. 3.1.2. Deciding how to extend the existing classes
      2. 3.2. Building the class
        1. 3.2.1. Creating the class file and constructor
        2. 3.2.2. Resetting the time and date
        3. 3.2.3. Accepting dates in common formats
          1. 3.2.3.1. Accepting a date in MM/DD/YYYY format
          2. 3.2.3.2. Accepting a date in DD/MM/YYYY format
          3. 3.2.3.3. Accepting a date in MySQL format
        4. 3.2.4. Outputting dates in common formats
        5. 3.2.5. Outputting date parts
        6. 3.2.6. Performing date-related calculations
          1. 3.2.6.1. Adding and subtracting days or weeks
          2. 3.2.6.2. Adding months
          3. 3.2.6.3. Subtracting months
          4. 3.2.6.4. Adding and subtracting years
          5. 3.2.6.5. Calculating the number of days between two dates
        7. 3.2.7. Creating a default date format
      3. 3.3. Creating read-only properties
      4. 3.4. Organizing and commenting the class file
      5. 3.5. Chapter review
    9. 4. USING PHP FILTERS TO VALIDATE USER INPUT
      1. 4.1. Validating input with the filter functions
        1. 4.1.1. Understanding how the filter functions work
          1. 4.1.1.1. filter_has_var()
          2. 4.1.1.2. filter_list()
          3. 4.1.1.3. filter_id()
        2. 4.1.2. Setting filter options
          1. 4.1.2.1. Filtering single variables
          2. 4.1.2.2. Setting flags and options when filtering a single variable
          3. 4.1.2.3. Filtering multiple variables
        3. 4.1.3. Setting a default filter
      2. 4.2. Building the validation class
        1. 4.2.1. Deciding what the class will do
        2. 4.2.2. Planning how the class will work
        3. 4.2.3. Coding the validation class properties and methods
          1. 4.2.3.1. Naming properties and defining the constructor
          2. 4.2.3.2. Setting the input type and checking required fields
          3. 4.2.3.3. Preventing duplicate filters from being applied to a field
          4. 4.2.3.4. Creating the validation methods
          5. 4.2.3.5. Creating the methods to process the tests and get the results
      3. 4.3. Using the validation class
      4. 4.4. Sticking to your design decisions
      5. 4.5. Chapter review
    10. 5. BUILDING A VERSATILE REMOTE FILE CONNECTOR
      1. 5.1. Designing the class
      2. 5.2. Building the class
        1. 5.2.1. Defining the constructor
        2. 5.2.2. Checking the URL
        3. 5.2.3. Retrieving the remote file
          1. 5.2.3.1. Defining the accessDirect() method
          2. 5.2.3.2. Using cURL to retrieve the remote file
          3. 5.2.3.3. Using a socket connection to retrieve the remote file
          4. 5.2.3.4. Handling the response headers from a socket connection
          5. 5.2.3.5. Generating error messages based on the status code
        4. 5.2.4. Final testing
        5. 5.2.5. Ideas for improving the class
      3. 5.3. Chapter review
    11. 6. SIMPLEXML—COULDN'T BE SIMPLER
      1. 6.1. A quick XML primer
        1. 6.1.1. What is XML?
        2. 6.1.2. How XML documents are structured
        3. 6.1.3. The rules of writing XML
          1. 6.1.3.1. Using HTML entities in XML
          2. 6.1.3.2. Inserting HTML and other code in XML
      2. 6.2. Using SimpleXML
        1. 6.2.1. Loading an XML document with SimpleXML
          1. 6.2.1.1. Loading XML from a file
          2. 6.2.1.2. Loading XML from a string
        2. 6.2.2. Extracting data with SimpleXML
          1. 6.2.2.1. Accessing text nodes
          2. 6.2.2.2. Accessing attributes
          3. 6.2.2.3. Accessing unknown nodes
      3. 6.3. Saving and modifying XML with SimpleXML
        1. 6.3.1. Outputting and saving SimpleXMLElement objects
        2. 6.3.2. Modifying SimpleXMLElement objects
          1. 6.3.2.1. Changing the values of text and attributes
          2. 6.3.2.2. Removing nodes and values
          3. 6.3.2.3. Adding attributes
          4. 6.3.2.4. Adding new elements
      4. 6.4. Using SimpleXML with namespaces
        1. 6.4.1. How namespaces are used in XML
        2. 6.4.2. Handling namespace prefixes in SimpleXML
          1. 6.4.2.1. Handling namespaced attributes
        3. 6.4.3. Finding out which namespaces a document uses
      5. 6.5. Using SimpleXML with XPath
        1. 6.5.1. A quick introduction to XPath
        2. 6.5.2. Using XPath to drill down into XML
          1. 6.5.2.1. Using XPath expressions for finer control
        3. 6.5.3. Using XPath with namespaces
          1. 6.5.3.1. Registering namespaces to work with XPath
      6. 6.6. Chapter review
    12. 7. SUPERCHARGED LOOPING WITH SPL
      1. 7.1. Introducing iterators
        1. 7.1.1. Using an array with SPL iterators
        2. 7.1.2. Limiting the number of loops with the LimitIterator
        3. 7.1.3. Using SimpleXML with an iterator
        4. 7.1.4. Filtering a loop with a regular expression
          1. 7.1.4.1. Setting options for RegexIterator
        5. 7.1.5. Looping sequentially through more than one set of data
        6. 7.1.6. Looking ahead with the CachingIterator
        7. 7.1.7. Using anonymous iterators as shorthand
      2. 7.2. Examining files and directories
        1. 7.2.1. Using DirectoryIterator
        2. 7.2.2. Including subdirectories in a single operation
        3. 7.2.3. Extracting file information with SplFileInfo
          1. 7.2.3.1. Finding files of a particular type
        4. 7.2.4. Reading and writing files with SplFileObject
      3. 7.3. Extending iterators
        1. 7.3.1. Understanding the Iterator interface
        2. 7.3.2. Extending the FilterIterator class
      4. 7.4. Chapter review
    13. 8. GENERATING XML FROM A DATABASE
      1. 8.1. Designing the application
        1. 8.1.1. Defining the application's purpose
        2. 8.1.2. Setting the requirements
      2. 8.2. Building the application
        1. 8.2.1. Creating the database connection
        2. 8.2.2. Getting the database result
          1. 8.2.2.1. Defining the properties and constructor
          2. 8.2.2.2. Implementing the Iterator interface
          3. 8.2.2.3. Implementing the Countable interface
        3. 8.2.3. Generating the XML output
          1. 8.2.3.1. Defining the properties and constructor
          2. 8.2.3.2. Setting the SQL query
          3. 8.2.3.3. Setting the root and top-level node names
          4. 8.2.3.4. Obtaining the primary key
          5. 8.2.3.5. Setting output file options
          6. 8.2.3.6. Using XMLWriter to generate the output
      3. 8.3. Chapter review
    14. 9. CASE STUDY: CREATING YOUR OWN RSS FEED
      1. 9.1. Understanding the RSS 2.0 format
        1. 9.1.1. The structure of an RSS 2.0 feed
          1. 9.1.1.1. What the <channel> element contains
          2. 9.1.1.2. What the <item> elements contain
        2. 9.1.2. Deciding what the feed will contain
      2. 9.2. Building the class
        1. 9.2.1. Populating the elements that describe the feed
        2. 9.2.2. Populating the <item> elements
          1. 9.2.2.1. Building the SQL query
          2. 9.2.2.2. Creating the <pubDate> element
          3. 9.2.2.3. Creating the <link> elements
          4. 9.2.2.4. Creating helper methods to format <item> child elements
          5. 9.2.2.5. Generating the XML for the <item> elements
      3. 9.3. Where to go from here