Free Sample
+ Collection

Object-Oriented Programming with PHP5

Hasin Hayder

Learn to leverage PHP5's OOP features to write manageable applications with ease
RRP $23.99
RRP $39.99
Print + eBook

Want this title & more?

$16.99 p/month

Subscribe to PacktLib

Enjoy full and instant access to over 2000 books and videos – you’ll find everything you need to stay ahead of the curve and make sure you can always get the job done.

Book Details

ISBN 139781847192561
Paperback272 pages

About This Book


  • 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

Who This Book Is For

From Beginners to Intermediate Users of PHP5

Table of Contents

Chapter 1: OOP vs. Procedural Programming
Introduction to PHP
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
Chapter 2: Kick-Starting OOP
Let's Bake Some Objects
Using an Object
Constructors and Destructors
Class Constants
Extending a Class [Inheritance]
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
Chapter 3: More OOP
Class Information Functions
Exception Handling
Array to Object
Accessing Objects in Array Style
Object Cloning
Autoloading Classes or Classes on Demand
Method Chaining
Life Cycle of an Object in PHP and Object Caching
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
Chapter 5: Reflection and Unit Testing
Unit Testing
Chapter 6: Standard PHP Library
Available Objects in SPL
Chapter 7: Database in an OOP Way
Introduction to MySQLi
Introduction to Data Abstraction Layers
Introduction to ActiveRecord
Chapter 8: Cooking XML with OOP
Formation of XML
Introduction to SimpleXML
Modifying Existing Documents
Other Useful Functions
Chapter 9: Building Better with MVC
What is MVC?
Planning for the Project
Designing the Bootstrap File
Adding Database Support
Building Applications over our Framework

What You Will Learn


  • 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.



Read More