Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Preface

Preface

PHP Master is aimed at intermediate PHP developers—those who have left their newbie status behind, and are looking to advance their skills and knowledge. Our aim as authors is to enable developers to refine their skills across a number of areas, and so we’ve picked topics that we felt have stood us in the best stead to grow as developers and progress our skills and careers.

It’s expected that you’ll already be working with at least some of the topics we cover; however, even topics that may already be familiar to you are recommended reading. PHP, perhaps more than many other languages, seems to attract people from different walks of life. There’s no sense of discrimination against those with no formal education in computing or in web development specifically. So while you may be actively using several techniques laid out here, dipping in to the chapters that follow could reveal new approaches, or illustrate some underlying theory that’s new to you. It is possible to go a long way with the tricks you pick up in your day-to-day work, but if you’re looking to cement those skills and gain a more solid footing, you’re in the right place.

This book will assist you in making that leap from competent web developer to confident software engineer—one who uses best practice, and gets the job done reliably and quickly. Because we’re writing PHP as a way to make a living, just like many of you do, we use a “how to” approach. The aim is to give you practical, useful advice with real examples as you move through the sections of the book.

Whatever path brought you here, we hope you find what you’re looking for, and wish you the best of everything as you travel onwards.

P.1. Who Should Read This Book

As stated, PHP Master is written for the intermediate developer. This means you should have a solid grounding in the fundamentals of PHP—the syntax underpinning the code, how functions and variables operate, constructs like foreach loops and if/else statements, and how server-side scripts interact with client-side markup (with HTML forms, for instance). We won’t be rehashing the basics—although there’ll be plenty of references to concepts you should already be familiar with, and you’ll be learning new ways to improve upon your existing techniques of generating server-side applications.

We’re going to work to an object oriented programming game plan—and if that’s a term you’ve heard mentioned before, you’ll certainly be hearing a lot more of it as you progress through this book! OOP, as it’s commonly known, is a standard to which good PHP developers adhere to ensure compliance with best practice, and to make their code work as efficiently as possible. You’ll learn how to use OOP to your advantage—creating classes, instantiating objects, and tightening your coding processes, generating some handy templates for future projects en route. If you’re already familiar with OOP, the opening chapter will serve as an excellent refresher, and if not, make sure you start right from the beginning to gain the most from reading PHP Master.

In addition, we’ll be working with databases—a key mode of storage for web applications. A basic understanding of what databases are and how they work will help you along, but we’ll be covering ways of connecting to them in great depth, as well as stepping through the world of MySQL—the most popular query language used to interact with information in a database.

Finally, this book will tackle some nifty approaches to refining, testing, and deploying your code. While these concepts are somewhat advanced, thorough explanations will be provided. A familiarity with command line interfaces and their associated vocabularies will be of assistance in these chapters.

P.2. What’s in This Book

This book comprises eight chapters and three appendices. While most chapters follow on from each other, they each deal with a new topic. You’ll probably gain the most benefit from reading them in sequence, but you can certainly skip around if you only need a refresher on a particular subject.


Chapter 1: Object Oriented Programming

We’ll start by discussing what object oriented programming consists of, and look at how to associate values and functions together in one unit: the object. Declaring classes and instantiating objects will be covered to start us off on our OOP journey; then we’ll delve into inheritance, interfaces, and exception handling. We’ll have a thorough OOP blueprint to work to by the end of this chapter.


Chapter 2: Databases

The Web is a dynamic world—gone are the days where users simply sit back and read web pages. Databases are a key component of interactive server-side development. In this chapter, we’ll discover how to connect to a database with the PDO extension, and how to store data and design database schema. In addition, we’ll look at the structured query language MySQL, as well as the commands you need to know to interact with a database.


Chapter 3: APIs

Application Programming Interfaces are a way of transferring data other than via web page-based methods; they provide the link that a particular service, application, or module exposes for others to interact with. We’ll look at how to incorporate them into your system, as well as investigate service-oriented architecture (SOA), HTTP requests and responses, and alternative web services.


Chapter 4: Design Patterns

In the real world, repeated tasks have best practices, and in coding, we call these design patterns; they help PHP users optimize development and maintenance. In this chapter, we’ll cover a wide range of design patterns, including singletons, factories, iterators, and observers. We’ll also take a tour of the MVC (Model-View-Controller) architecture that underpins a well-structured application.


Chapter 5: Security

All technologies have some level of capability for misuse in the hands of those with ill intentions, and every good programmer must know the best techniques for making their systems as secure as possible—after all, your clients will demand it. In this chapter, we’ll cover a broad range of known attack vectors—including cross-site scripting, session hijacking, and SQL injection—and how to protect your application from malicious entry. We’ll learn how to hash passwords and repel brute force attacks, as well as dissect the PHP mantra: “filter input, escape output.”


Chapter 6: Performance

The bigger your application becomes, the greater the need to test its performance capabilities. Here we’ll learn how to “stress test” our code using tools like ApacheBench and JMeter, the best way of optimizing our server configuration, and cover strategies for streamlining file systems and profiling your code’s actions.


Chapter 7: Automated Testing

As the functionality of an application changes, so does its definition of correct behavior. The purpose of automated testing is to assure that your application’s intended behavior and its actual behavior are consistent. In this chapter, we’ll learn how to target specific facets of your application with unit testing, database testing, systems testing, and load testing.


Chapter 8: Quality Assurance

Of course, all the hard work you’ve put into creating your application shouldn’t go to waste; you want your project to be of a high standard. In this chapter, we’ll look at measuring quality with static analysis tools, resources you can use to maintain best-practice coding standards and perfect your documentation, and robust methods of deploying your project on the Web.


Appendix A: PEAR and PECL

So many of the tools we refer to reside in the PEAR and PECL repositories, and yet we’ve met plenty of PHP developers who are yet to use them. In this appendix, we provide full instructions for setting these up, so there’s no longer an excuse for being ignorant of the jewels within.


Appendix B: SPL: The Standard PHP Library

The Standard PHP Library is a fabulous and under-celebrated extension that ships as standard with PHP and contains some very powerful tools to include in your application. This is especially worth a read as a follow-on to the OOP and Design Patterns chapters.


Appendix C: Next Steps

Where to from here? A good PHP developer never stops improving their skill set, and here you’ll find a handy list of resources, from community groups to conferences.

P.3. Where to Find Help

SitePoint has a thriving community of web designers and developers ready and waiting to help you out if you run into trouble. We also maintain a list of known errata for the book, which you can consult for the latest updates.

P.3.1. The SitePoint Forums

The SitePoint Forums are discussion forums where you can ask questions about anything related to web development. You may, of course, answer questions too. That’s how a forum site works—some people ask, some people answer, and most people do a bit of both. Sharing your knowledge benefits others and strengthens the community. A lot of interesting and experienced web designers and developers hang out there. It’s a good way to learn new stuff, have questions answered in a hurry, and generally have a blast.

P.3.2. The Book’s Website

Located at http://www.sitepoint.com/books/phppro/, the website that supports this book will give you access to the following facilities:

P.3.2.1. The Code Archive

As you progress through this book, you’ll note a number of references to the code archive. This is a downloadable ZIP archive that contains the example source code printed in this book. If you want to cheat (or save yourself from carpal tunnel syndrome), go ahead and download the archive.

P.3.2.2. Updates and Errata

No book is perfect, and we expect that watchful readers will be able to spot at least one or two mistakes before the end of this one. The Errata page on the book’s website will always have the latest information about known typographical and code errors.

P.4. The SitePoint Newsletters

In addition to books like this one, SitePoint publishes free email newsletters, such as the SitePoint Tech Times, SitePoint Tribune, and SitePoint Design View, to name a few. In them, you’ll read about the latest news, product releases, trends, tips, and techniques for all aspects of web development. Sign up to one or more SitePoint newsletters at http://www.sitepoint.com/newsletter/.

P.5. The SitePoint Podcast

Join the SitePoint Podcast team for news, interviews, opinion, and fresh thinking for web developers and designers. We discuss the latest web industry topics, present guest speakers, and interview some of the best minds in the industry. You can catch up on the latest and previous podcasts at http://www.sitepoint.com/podcast/, or subscribe via iTunes.

P.6. Your Feedback

If you’re unable to find an answer through the forums, or if you wish to contact us for any other reason, the best place to write is books@sitepoint.com. We have a well-staffed email support system set up to track your inquiries, and if our support team members can’t answer your question, they’ll send it straight to us. Suggestions for improvements, as well as notices of any mistakes you may find, are especially welcome.

P.7. Acknowledgments

P.7.1. Lorna Mitchell

I’d like to say a big thank you to the friends who told me to stop talking about writing a book, and just write one. I’d also like to thank those who tricked me into realizing that I could write, even though I thought I was a software developer. The team at SitePoint were wonderful, not just with the words that I wrote but also with getting me through the writing process, as I was a complete newbie! And last but very definitely not least, my co-authors, whom I’m proud to call friends, and who shared this experience with me—rock stars, both of you.

P.7.2. Davey Shafik

First and foremost, I want to say a big thank you to my wife, Frances, for putting up with the late nights and lost weekends that went into this book. I’d also like to thank my very talented co-authors, who I’m fortunate to be able to consider great friends. Thank you to the great team at SitePoint for their efforts in putting together this great book. Finally, thank you to you, the reader, for taking the time to read this book; I hope it not only answers some questions, but opens your mind to many more to come.

P.7.3. Matthew Turland

I found PHP in 2002, and later its community around 2006. I came for the technology, but stayed for the people. It’s been one of the best communities I’ve found in my time as a software developer and I’m privileged to be a part of it. Thanks to everyone who’s shared in that experience with me, especially those who have befriended and guided me over the years. Thanks to my spectacular co-authors, Lorna and Davey; I could not have asked for better partners in this project, nor better friends with which to share it. Thanks to the excellent SitePoint team of Kelly Steele, Tom Museth, Sarah Hawk, and Lisa Lang, who helped bring us and the pieces of this project together to produce the polished book that you see now. Thanks also to our reviewer Luke Cawood, and my friends Paddy Foran and Mark Harris, all of whom provided feedback on the book as it was being written. Finally, thanks to you, the reader; I hope you enjoy this book and that it helps to bring you forward with PHP.

P.8. Conventions Used in This Book

You’ll notice that we’ve used certain typographic and layout styles throughout the book to signify different types of information. Firstly, because this is a book about PHP, we’ve dispensed with the opening and closing tags (<?php and ?>) in most code examples and assumed you’ll have them inserted in your own files. The only exception is where PHP is printed alongside, say, XML or HTML.

Look out for the following items:

P.8.1. Code Samples

Code in this book will be displayed using a fixed-width font, like so:

class Courier { public function __construct($name) {
      $this->name = $name; return true; } }

If the code is to be found in the book’s code archive, the name of the file will appear at the top of the program listing, like this:

listing P-1. example.php

function __autoload($classname) { include
        strtolower($classname) . '.php'; }

If only part of the file is displayed, this is indicated by the word excerpt:

listing P-2. example.php (excerpt)

$mono = new Courier('Monospace
        Delivery');

If additional code is to be inserted into an existing example, the new code will be displayed in bold:

function animate() { new_variable =
      "Hello"; }

Where existing code is required for context, rather than repeat all the code, a vertical ellipsis will be displayed:

function animate() { ⋮ return
      new_variable; }

Some lines of code are intended to be entered on one line, but we’ve had to wrap them because of page constraints. A ➥ indicates a line break that exists for formatting purposes only, and should be ignored:

URL.open("http://www.sitepoint.com/blogs/2007/05/28/user-style-she
      ➥ets-come-of-age/");

P.8.2. Tips, Notes, and Warnings


Tip:

Hey, You!

Tips will give you helpful little pointers.



Note:

Notes are useful asides that are related—but not critical—to the topic at hand. Think of them as extra tidbits of information.



Note:

… pay attention to these important points.



Warning:

Watch Out!

Warnings will highlight any gotchas that are likely to trip you up along the way.