PHP Object-Oriented Solutions

( 2 )


With the surge of popularity of PHP 5, 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 prepackaged scripts that you can incorporate into your existing sites with the minimum of ...

See more details below
$26.12 price
(Save 29%)$36.99 List Price

Pick Up In Store

Reserve and pick up in 60 minutes at your local store

Other sellers (Paperback)
  • All (21) from $4.41   
  • New (11) from $21.29   
  • Used (10) from $4.41   
Sending request ...


With the surge of popularity of PHP 5, 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 prepackaged scripts that you can incorporate into your existing sites with the minimum of effort.

It shows how object-oriented programming 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
  • Prepackaged scripts can easily be added to your own projects

What you’ll learn

  • PHP features, such as the Standard PHP Library (SPL), that are poorly documented or ignored by existing books
  • How to develop classes of their own
  • OOP in easy-to-understand language without getting bogged down in dense theory
  • Solid foundations for developers wishing to delve more deeply into OOP
  • How to leverage the strengths of OOP as a means for creating reusable code that can be used successfully within a procedural context
  • How to code for both PHP 5 and 6

Who this book is for

This book is aimed at intermediate-level PHP developers who want to take their skills to the next level by exploring object-oriented programming as a way of creating reusable code for a everyday website tasks. A typical reader would be someone who has read one of the author's previous titles, such as PHP Solutions, or one of the PHP books for Dreamweaver and feels ready to tackle something a little more challenging.

Read More Show Less

Product Details

  • ISBN-13: 9781430210115
  • Publisher: Apress
  • Publication date: 8/21/2008
  • Edition number: 1
  • Pages: 392
  • Sales rank: 964,875
  • Product dimensions: 7.50 (w) x 8.90 (h) x 0.90 (d)

Meet the Author

David Powers is an Adobe Community Expert for Dreamweaver and author of a series of highly successful books on PHP, including PHP Solutions: Dynamic Web Design Made Easy and Foundation PHP for Dreamweaver 8. As a professional writer, he has been involved in electronic media for more than 30 years, first with BBC radio and television and more recently with the Internet. His clear writing style is valued not only in the English-speaking world; several of his books have been translated into Spanish and Polish. What started as a mild interest in computing was transformed almost overnight into a passion, when David was posted to Japan in 1987 as BBC correspondent in Tokyo. With no corporate IT department just down the hallway, he was forced to learn how to fix everything himself. When not tinkering with the innards of his computer, he was reporting for BBC television and radio on the rise and collapse of the Japanese bubble economy. Since leaving the BBC to work independently, he has built up an online bilingual database of economic and political analysis for Japanese clients of an international consultancy. When not pounding the keyboard writing books or dreaming of new ways of using PHP and other programming languages, David enjoys nothing better than visiting his favorite sushi restaurant. He has also translated several plays from Japanese.
Read More Show Less

Table of Contents

About the Author     xi
About the Technical Reviewer     xiii
Acknowledgments     xv
Introduction     xvii
Why Object-Oriented PHP?     3
Understanding basic OOP concepts     4
How OOP evolved     5
Using classes and objects     6
Protecting data integrity with encapsulation     8
Polymorphism is the name of the game     10
Extending classes through inheritance     10
Deciding on a class hierarchy     11
Using best practice     12
How OOP has evolved in PHP     13
OOP since PHP 5     13
Preparing for PHP 6     14
Choosing the right tools to work with PHP classes     16
Using a specialized script editor     16
Chapter review     19
Writing PHP Classes     23
Formatting code for readability     25
Using the Zend Framework PHP Coding Standard     25
Choosing descriptive names for clarity     26
Creating classes and objects     26
Defining a class     27
Controlling access to properties and methods     27
Quick review     32
Setting default valueswith a constructor method     33
Using inheritance to extend a class     36
Defining a child class     37
Accessing a parent class's methods and properties     39
Using the scope resolution operator     39
Controlling changes to methods and properties     44
Preventing a class or method from being overridden     44
Using class constants for properties     46
Creating static properties and methods     47
Quick review     49
Loading classes automatically     50
Exploring advanced OOP features     51
Creating abstract classes and methods     52
Simulating multiple inheritance with interfaces     54
Understanding which class an object is an instance of     55
Restricting acceptable data with type hinting     56
Using magic methods     59
Converting an object to a string     60
Cloning an object     60
Accessing properties automatically     64
Accessing methods automatically     65
Cleaning up with a destructor method     66
Handling errors with exceptions     67
Throwing an exception     67
Catching an exception      67
Extracting information from an exception     68
Extending the Exception class     72
Using comments to generate code hints     73
Writing PHPDoc comments     74
Chapter review     75
Taking the Pain Out of Working with Dates     77
Designing the class     78
Examining the built-in date-related classes     79
Using the DateTime class     81
Setting the default time zone in PHP     83
Examining the DateTimeZone class     85
Using the DateTimeZone class     87
Deciding how to extend the existing classes     89
Building the class     91
Creating the class file and constructor     91
Resetting the time and date     95
Accepting dates in common formats     98
Accepting a date in MM/DD/YYYY format     98
Accepting a date in DD/MM/YYYY format     99
Accepting a date in MySQL format     99
Outputting dates in common formats     100
Outputting date parts     101
Performing date-related calculations     103
Adding and subtracting days or weeks     105
Adding months     106
Subtracting months      110
Adding and subtracting years     112
Calculating the number of days between two dates     113
Creating a default date format     114
Creating read-only properties     115
Organizing and commenting the class file     117
Chapter review     118
Using PHP Filters to Validate User Input     121
Validating input with the filter functions     122
Understanding how the filter functions work     123
filter_has_var()     125
filter_list()     126
filter_id()     126
Setting filter options     127
Filtering single variables     130
Setting flags and options when filtering a single variable     134
Filtering multiple variables     136
Setting a default filter     137
Building the validation class     138
Deciding what the class will do     138
Planning how the class will work     139
Coding the validation class properties and methods     140
Naming properties and defining the constructor     140
Setting the input type and checking required fields     142
Preventing duplicate filters from being applied to a field     147
Creating the validation methods     147
Creating the methods to process the tests and get the results     157
Using the validation class     159
Sticking to your design decisions     165
Chapter review     166
Building a Versatile Remote File Connector     169
Designing the class     171
Building the class     172
Defining the constructor     172
Checking the URL     174
Retrieving the remote file     180
Defining the accessDirect() method     180
Using cURL to retrieve the remote file     186
Using a socket connection to retrieve the remote file     190
Handling the response headers from a socket connection     196
Generating error messages based on the status code     202
Final testing     204
Ideas for improving the class     204
Chapter review     205
SimpleXML-Couldn't Be Simpler     207
A quick XML primer     208
What is XML?     208
How XML documents are structured     210
The rules of writing XML     212
Using HTML entities in XML     213
Inserting HTML and other code in XML     213
Using SimpleXML     214
Loading an XML document with SimpleXML     217
Loading XML from a file     217
Loading XML from a string     218
Extracting data with SimpleXML     220
Accessing text nodes     221
Accessing attributes     221
Accessing unknown nodes     222
Saving and modifying XML with SimpleXML     228
Outputting and saving SimpleXMLElement objects     228
Modifying SimpleXMLElement objects     231
Changing the values of text and attributes     231
Removing nodes and values     232
Adding attributes     233
Adding new elements     234
Using SimpleXML with namespaces     235
How namespaces are used in XML     236
Handling namespace prefixes in SimpleXML     236
Handling namespaced attributes     241
Finding out which namespaces a document uses     242
Using SimpleXML with XPath     244
A quick introduction to XPath     244
Using XPath to drill down into XML     245
Using XPath expressions for finer control     246
Using XPath with namespaces     247
Registering namespaces to work with XPath      247
Chapter review     248
Supercharged Looping with SPL     251
Introducing iterators     252
Using an array with SPL iterators     253
Limiting the number of loops with the LimitIterator     253
Using SimpleXML with an iterator     255
Filtering     256
Setting options for RegexIterator     259
Looping sequentially through more than one set of data     263
Looking ahead with the CachingIterator     265
Using anonymous iterators as shorthand     268
Examining files and directories     269
Using DirectoryIterator     270
Including subdirectories in a single operation     271
Extracting file information with SplFileInfo     273
Finding files of a particular type     274
Reading and writing files with SplFileObject     275
Extending iterators     281
Understanding the Iterator interface     282
Extending the FilterIterator class     283
Chapter review     285
Generating XML from a Database     289
Designing the application     290
Defining the application's purpose     290
Setting the requirements      292
Building the application     292
Creating the database connection     293
Getting the database result     294
Defining the properties and constructor     295
Implementing the Iterator interface     296
Implementing the Countable interface     298
Generating the XML output     302
Defining the properties and constructor     303
Setting the SQL query     305
Setting the root and top-level node names     305
Obtaining the primary key     306
Setting output file options     307
Using XMLWriter to generate the output     307
Chapter review     317
Case Study: Creating Your Own RSS Feed     321
Understanding the RSS 2.0 format     322
The structure of an RSS 2.0 feed     322
What the [left angle bracket]channel[right angle bracket] element contains     323
What the [left angle bracket]item[right angle bracket] elements contain     325
Deciding what the feed will contain     326
Building the class     327
Populating the elements that describe the feed     328
Populating the [left angle bracket]item[right angle bracket] elements     333
Building the SQL query      334
Creating the [left angle bracket]pubDate[right angle bracket] element     338
Creating the [left angle bracket]link[right angle bracket] elements     340
Creating helper methods to format [left angle bracket]item[right angle bracket] child elements     344
Generating the XML for the [left angle bracket]item[right angle bracket] elements     346
Where to go from here     352
Index     355
Read More Show Less

Customer Reviews

Average Rating 4
( 2 )
Rating Distribution

5 Star


4 Star


3 Star


2 Star


1 Star


Your Rating:

Your Name: Create a Pen Name or

Barnes & Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation


  • - By submitting a review, you grant to Barnes & and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Terms of Use.
  • - Barnes & reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously
Sort by: Showing all of 2 Customer Reviews
  • Anonymous

    Posted November 2, 2009

    No text was provided for this review.

  • Anonymous

    Posted April 20, 2010

    No text was provided for this review.

Sort by: Showing all of 2 Customer Reviews

If you find inappropriate content, please report it to Barnes & Noble
Why is this product inappropriate?
Comments (optional)