Object-Oriented Programming with PHP5

Object-Oriented Programming with PHP5
eBook: $23.99
Formats: PDF, PacktLib, ePub and Mobi formats
save 40%!
Print + free eBook + free PacktLib access to the book: $63.98    Print cover: $39.99
save 37%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Table of Contents
Sample Chapters


  • General OOP concepts explained
  • Implement Design Patterns in your applications and solve common OOP Problems
  • Take full advantage of native built-in objects
  • Test your code by writing unit tests with PHPUnit

Book Details

Language : English
Paperback : 272 pages [ 235mm x 191mm ]
Release Date : December 2007
ISBN : 1847192564
ISBN 13 : 9781847192561
Author(s) : Hasin Hayder
Topics and Technologies : All Books, Web Development, Open Source, PHP

Table of Contents

Chapter 1: OOP vs. Procedural Programming
Chapter 2: Kick-Starting OOP
Chapter 3: More OOP
Chapter 4: Design Patterns
Chapter 5: Reflection and Unit Testing
Chapter 6: Standard PHP Library
Chapter 7: Database in an OOP Way
Chapter 8: Cooking XML with OOP
Chapter 9: Building Better with MVC
  • Chapter 1: OOP vs. Procedural Programming
    • Introduction to PHP
      • Ready, Set, Go
    • A Little History of OOP in PHP
    • Procedural vs. OO Coding Style
    • Benefits of OOP
    • Dissection of an Object
    • Difference of OOP in PHP4 and PHP5
    • Some Basic OO Terms
    • General Coding Conventions
    • Summary
  • Chapter 2: Kick-Starting OOP
    • Let's Bake Some Objects
      • Accessing Properties and Methods from Inside the Class
    • Using an Object
    • Modifiers
    • Constructors and Destructors
    • Class Constants
    • Extending a Class [Inheritance]
      • Overriding Methods
      • Preventing from Overriding
      • Preventing from Extending
    • Polymorphism
    • Interface
    • Abstract Class
    • Static Method and Properties
    • Accessor Methods
    • Using Magic Methods to Set/Get Class Properties
    • Magic Methods for Overloading Class Methods
    • Visually Representing a Class
    • Summary
  • Chapter 3: More OOP
    • Class Information Functions
      • Checking if a Class Already Exists
      • Finding Currently Loaded Classes
      • Finding out if Methods and Properties Exists
      • Checking the Type of Class
      • Finding Out the Class Name
    • Exception Handling
      • Collecting all PHP Errors as Exception
    • Iterators
    • ArrayObject
    • Array to Object
    • Accessing Objects in Array Style
    • Serialization
      • Magic Methods in Serialization
    • Object Cloning
    • Autoloading Classes or Classes on Demand
    • Method Chaining
    • Life Cycle of an Object in PHP and Object Caching
    • Summary
  • Chapter 4: Design Patterns
    • You Might have Done this Before…
    • Strategy Pattern
    • Factory Pattern
    • Abstract Factory
    • Adapter Pattern
    • Singleton Pattern
    • Iterator Pattern
    • Observer Pattern
    • Proxy Pattern or Lazy Loading
    • Decorator Pattern
    • Active Record Pattern
    • Facade Pattern
    • Summary
  • Chapter 5: Reflection and Unit Testing
    • Reflection
      • ReflectionClass
    • ReflectionMethod
    • ReflectionParameter
    • ReflectionProperty
    • Unit Testing
      • Benefits of Unit Testing
      • A small Introduction to Vulnerable Bugs
      • Preparing for Unit Testing
      • Starting Unit Testing
      • Testing an Email Validator Object
      • Unit Testing for Everyday Script
      • Test Driven Development
        • Writing Multiple Assertions
      • PHPUnit API
    • Summary
  • Chapter 6: Standard PHP Library
    • Available Objects in SPL
    • ArrayObject
    • ArrayIterator
    • DirectoryIterator
    • RecursiveDirectoryIterator
    • RecursiveIteratorIterator
    • AppendIterator
    • FilterIterator
    • LimitIterator
    • NoRewindIterator
    • SeekableIterator
    • RecursiveIterator
    • SPLFileObject
    • SPLFileInfo
    • SPLObjectStorage
    • Summary
  • Chapter 7: Database in an OOP Way
    • Introduction to MySQLi
      • Connecting to MySQL in an OO Way
      • Selecting Data in an OO Way
      • Updating Data in an OO Way
      • Prepared Statements
        • Basic Prepared Statements
        • Prepared Statements with Variables
      • Using BLOB with Prepared Statements
      • Executing Stored Procedure with MySQLi and PHP
    • PDO
      • DSN Settings for Different Databases Engines
      • Using Prepared Statements with PDO
      • Calling Stored Procedures
      • Other Interesting Functions
    • Introduction to Data Abstraction Layers
      • ADOdb
        • Installing ADOdb
        • Connecting to Different Databases
        • Basic Database Operations using ADOdb
        • Inserting, Deleting, and Updating Records
        • Executing Prepared Statements
      • MDB2
        • Installing MDB2
        • Connecting to Database
        • Executing Prepared Statements
    • Introduction to ActiveRecord
      • Creating a New Record via ActiveRecord
      • Selecting and Updating Data
    • Summary
  • Chapter 8: Cooking XML with OOP
    • Formation of XML
    • Introduction to SimpleXML
      • Parsing Documents
      • Accessing Attributes
      • Parsing Flickr Feeds using SimpleXML
      • Managing CDATA Sections using SimpleXML
      • XPath
      • DOM API
    • Modifying Existing Documents
    • Other Useful Functions
    • Summary
  • Chapter 9: Building Better with MVC
    • What is MVC?
    • Planning for the Project
    • Designing the Bootstrap File
    • Adding Database Support
      • Drivers
    • Building Applications over our Framework
      • Authentication Controller
    • Summary

Hasin Hayder

Hasin Hayder graduated in Civil Engineering from the Rajshahi University of Engineering and Technology (RUET) in Bangladesh. He is a Zend-certified Engineer and expert in developing localized applications. He is currently working as a Technical Director in Trippert Labs and managing the local branch in Bangladesh. Beside his full time job, Hasin writes his blog at http://hasin.wordpress.com, writes article in different websites and maintains his open source framework Orchid at http://orchid.phpxperts.net. Hasin lives in Bangladesh with his wife Ayesha and his son, Afif.

Sorry, we don't have any reviews for this title yet.

Code Downloads

Download the code and support files for this book.

Submit Errata

Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.


- 4 submitted: last submission 11 Nov 2013

Errata type: Others | Page number: 13 | Errata date: 28 Apr 08

In the paragraph 'dissection of an object', data structure' is repeated twice


Errata type: Typo | Page number: 22 | Errata date: 13 Nov 08

The PHP code fragment that reads $emailer = new emailer(); should read $emailer = new Emailer();


Errata type: code | Page number: 60 | Errata date: 28 Apr 08

In the function __wakeup() the last statement $this->staticvars = array(); must be removed


Errata type: Code | Page number: 72

class DBManager
public static function setDriver($driver)
$this->driver = $driver;
//set the driver

It should be
class DBManager
public static function setDriver($driver)
self::$driver = $driver;
//set the driver

Sample chapters

You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

Frequently bought together

Object-Oriented Programming with PHP5 +    Mastering Web Application Development with AngularJS =
50% Off
the second eBook
Price for both: $34.95

Buy both these recommended eBooks together and get 50% off the cheapest eBook.

What you will learn from this book


  • Understand the OOP features of PHP clearly
  • Write manageable code

Chapter 1 introduces object-oriented programming and how it fits for PHP. Some benefits of functional programming over procedural programming are highlighted.
In Chapter 2 you learn to create objects and define their properties and methods. Details of classes, properties, and methods follow, along with the scope of methods. This chapter shows you the benefits of using interfaces and a few other basic OOP features in PHP to kick start your journey through OOPing in PHP.

Now that you have got your basics done for OOP in PHP, Chapter 3 helps you to strengthen your base. It helps you to deal with more details and some advanced features. For example you learn about class information functions, using which you can investigate details of any class. This chapter takes you through some handy object-oriented information functions, exception handling, iterators, and storing objects using serialization.

In Chapter 4 you learn some of the Design Patterns and how to implement them in PHP. These are an essential part of OOP and make your code more effective, better performing, and easier to maintain. Sometimes we implement these design patterns in our code without knowing that these solutions are defined by design patterns. Proper usage of the correct pattern can make your code perform better; similarly using them improperly could make your code slower and less efficient.

Chapter 5 focuses on two very important features of object-oriented programming in PHP, reflection and unit testing. PHP5 replaces many old APIs with smarter new ones. One of these is the Reflection API, with which you can reverse or engineer any class or object to figure out its properties and methods. You can invoke those methods dynamically and more. Unit testing is an essential part of good, stable, and manageable application design. We focus on one very popular package, PHPUnit, which is a port of JUnit to PHP. If you follow the guidelines provided in this chapter you will be able to design your own unit tests successfully.

Some built-in objects and interfaces in PHP make life much easier for PHP developers. In Chapter 6 you will learn about the huge object repository named the Standard PHP Library or SPL.
Chapter 7: In this chapter we discuss the improved MySQL API known as MySQLi and take a basic look at PHP Data Objects (PDO), adoDB, and PEAR::MDB2. We take a look at the Active Record pattern in PHP using adoDB’s active record library and the Object-Relational Mapping (ORM) pattern using Propel. We focus on some specific topics that are interesting for PHP developers doing database access the OO way.

In Chapter 8, you learn to process XML with PHP. You get to know about different APIs like the SimpleXML API to read XML and the DOMDocument object to parse and create XML documents.

Chapter 9: In Chapter 4 you learned how design patterns can simplify your daily life in programming by providing you with a common approach for solving problems. One of the most used design patterns for application architecture is the Model-View-Controller (MVC) pattern. In this chapter we discuss the basic structure of MVC frameworks and then introduce you to some of these popular frameworks. Frameworks play a very important role in Rapid Development of PHP applications. You will learn how to build a framework in this chapter, which will also help you to understand object loading, data abstraction layers, and the importance of separation and finally you get a closer look at how applications are done.

In Detail

Some basic objected-oriented features were added to PHP3; with PHP5 full support for object-oriented programming was added to PHP. Object-oriented programming was basically introduced to ease the development process as well as reduce the time of development by reducing the amount of code needed. OOP can greatly improve the performance of a properly planned and designed program.

This book covers all the general concepts of OOP then shows you how to make use of OOP in PHP5, with the aid of an ample number of examples.

This book will help you to understand the core object-oriented programming concepts with PHP. PHP is one of the most popular languages for web application development, and PHP5 supports OOP very well. This book will help you to master core OOP features in PHP as well as advanced Topics like Design Patterns including Model-View-Controller (MVC), and Unit Testing. It includes suggestions to help you to write good code. Moreover this book provides comprehensive documentation and working examples on the Standard PHP Library (SPL), which are hard to find elsewhere. It will really help you to leverage PHP’s OOP features to write manageable applications with ease.


This book explains how to take full advantage of PHP’s OOP features in simple language. It contains plentiful examples to help you to understand OOP.

Who this book is for

From Beginners to Intermediate Users of PHP5

Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software