PHP This!
A Beginners Guide to Learning Object Oriented PHP
Michelle Gosney
PHP This! A Beginners Guide to Learning Object Oriented PHP, First Edition
Copyright © 2013 by Michelle Gosney
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner.
Published in eBook format by eBookIt.com
ISBN-13 (electronic): 978-1-4566-1529-1
Introduction
A few years ago when I first sat down to evaluate the new features of PHP 5.3 I knew I was facing a challenging learning curve. It had been about 9 years since I had used PHP and at that time it was an easy to use, loosely typed, scripting language. My first thought while exploring the new features of PHP 5.3 was that “wow, little PHP is all grown up.”
In an effort to get caught up in a hurry, I looked at several books and on-line forums and quickly grew frustrated with the same boring and monotonous way that these advanced concepts were explained and the poor examples that were used. Having an extensive background in object-oriented programming I continuously thought to myself “what if someone is trying to learn these concepts without much of an object-oriented background?”
The purpose of this book is to explain the advanced features and concepts of object-oriented PHP in a way that is simple and easy to understand and in a fashion that will hopefully help you remember and apply what you have learned.
If you are preparing for a job interview and you expect questions about advanced features of PHP 5+ this book will help you. In fact, you can be assured that most of the key concepts that are discussed in this book will come up in a job interview. I endeavored to include a few other features that you may be quizzed on in an interview as well.
Why Read This Book
This book is intended for developers who are new to Object Oriented PHP and perhaps Object Oriented Programming in general and are having difficulty grasping the new concepts. There are several reasons for reading this book beginning with the job description below. This book will help you understand and remember Object Oriented concepts as applied to PHP and give you a solid platform for continuous learning. If you ever find yourself in an interview for a job like the description below you will know what you are talking about and how to answer the fundamental questions about PHP that employers most likely will throw at you.
Figure 1-1. Jr. PHP Developer Job Description
This book focuses on the advanced features of Object Oriented PHP which can be applied to advanced development methods such as Extreme Programming, Design-by-Contract, Test Driven Development, Coding-to-the-Interface and Agile development. During the course of this brief book many simple examples will be used to illustrate key concepts and how they work. We will also build a small database driven application, a test harness for the application using PHPUnit, and add version control of our source code using Subversion (SVN).
In reference to the Job Description above, this book doesn’t discuss JQuery or any other Javascript library. I do however encourage you to get a good JQuery book. Javascript was practically useless ten years ago as a cross browser, client-side development tool but has gained prominence and will continue to be pertinent for many years to come. When shopping for a JQuery book, keep in mind that JQuery’s features are essentially broken down across eight major categories: Core Functionality, DOM Selection & Traversal, DOM Manipulation & CSS, Events, Effects & Animations, AJAX, User Interface and Extensibility through Plug-ins. It would be best to find a book that comprehensively covers each of these categories.
Why Learn Object Oriented PHP
There are several reasons to learn object-oriented programming. I will briefly touch on six primary advantages.
First of all, learning object-oriented programming, whether in PHP or any other OO-based language, will increase your value as a programmer which ultimately means you can make more money and have more flexibility on where you work.
Secondly, object-oriented principles are consistent across all of the object-oriented based languages and platforms. Once you have learned Object-Oriented PHP, grasping other languages will be much easier. Whether it is C#, Java, Visual Basic, Ruby on Rails or even C++, you will already know most of what you need to know. The syntax and semantics may differ slightly from language to language but the way they all work is essentially the same.
A third advantage to learning Object-Oriented programming is scalability. Using OO-based PHP for a small project may seem verbose and excessive when simple scripting can accomplish the same goal. However, if or when that small application grows bigger and bigger and becomes more complex the time spent writing the base code in Object-Oriented PHP will evidently prove to be a good investment.
A fourth advantage is that OO-based PHP creates re-usable blocks of code that can be used in your current project and in future projects. You can eventually accumulate a reusable and robust code library.
The fifth advantage to using OO-based PHP is that it is much easier to have multiple programmers working on the same project or code base because everything is segmented into objects. Various programmers can be assigned different objects to work on without worrying about touching or breaking code in other parts of the same project. This is considering that these objects are designed properly from the outset. It is this aspect of Object-Oriented PHP that makes PHP development in an SCRUM/Agile environment possible and allows robust tools like SVN and PHPUnit to be used. Learning how to use these tools in a team environment will certainly boost your value as a programmer.
The sixth reason for learning Object-Oriented PHP is that it allows you to leverage Object-Oriented based resources like PHP frameworks such as Zend and libraries such as PEAR. Learning how to use these resources effectively can help you build powerful applications faster and more efficiently. This is especially helpful if you are a freelance developer because you can produce robust applications faster and balance multiple projects easier which mean more income.
Chapter 1: PHP Objects & Classes
Initial Confusion
The real problem in explaining object-oriented programming is that there is no absolute linear or sequential path to doing so, the key concepts are all global. They say men are linear thinkers and women are global thinkers. Men tend to look for a starting point and put things in sequential order, doing a single task at a time. Women are global thinkers in that they are pervasive, considering the big picture. The fundamental concepts of object-oriented programming are global in that way versus straight procedural programming which is clearly linear.
This is the benefit of OOP however it doesn't help in explaining it to someone who is new to it. Most teachers will start with the 3 main advantages of OOP: encapsulation, inheritance and polymorphism and then elaborate on them. This can be confusing.
PHP Objects & Classes
At this point I would explain what an object is but you can't have an object without a class function. A class is essentially a blueprint for an object. It is a user-defined data type similar to a C-struct but more complex. A single instance of a class is an "object.”
A class defines characteristics that an object will have called properties. Properties describe an object and can be assigned values. For