Expert PHP 5 ToolsProven enterprise development tools and best practices for designing, coding, testing, and deploying PHP applications Dirk Merkel BIRMINGHAM - MUMBAI... Neither the au
Trang 2Expert PHP 5 Tools
Proven enterprise development tools and best
practices for designing, coding, testing, and deploying PHP applications
Dirk Merkel
BIRMINGHAM - MUMBAI
Trang 3Expert PHP 5 Tools
Copyright © 2010 Packt Publishing
All rights reserved No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews
Every effort has been made in the preparation of this book to ensure the accuracy
of the information presented However, the information contained in this book is sold without warranty, either express or implied Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals However, Packt Publishing cannot guarantee the accuracy of this information.First published: April 2010
Trang 4Cover Work
Melwyn D'sa
Trang 5About the Author
Dirk Merkel has been developing software in a variety of programming
languages for many years, including PHP, Java, Perl, and Ruby His focus has been on web-related technologies and he has over 10 years experience coding in PHP He has experience covering the whole software development cycle and has been managing several teams of developers working on large-scale projects
He has been working as a professional consultant through his company
Waferthin Web Works LLC (http://www.waferthin.com) and can be reached
at dirk@waferthin.com He is also the Chief Technology Officer at VivanTech Inc.,
a San Diego based provider of IT solutions
He has written several articles on software development and security
Expert PHP 5 Tools is his first book.
He lives in San Diego with his lovely wife and two wonderful daughters
I would like to thank my family (near and far)—especially my
parents and sisters, my lovely wife, Rania, and my two awesome
daughters, Nadia and Yasmin
Trang 6About the Reviewers
Andrew J Peterson lives with his wife and three daughters in San Francisco, California He has 20 years experience building and managing software systems for consumers, enterprises, start-ups, and non-profits He brings expertise in the full life-cycle of software development, engineering, methodologies, architecture, and usability He has diverse experience in the industry In the consumer space,
he led a team in the creation of the top-selling SoundEdit 16 He served numerous roles producing enterprise software, for the leading supplier of software solutions for container terminals, shipping ports and lines, and distribution centers Over the past ten years, he transferred this experience to web-based software He has built a variety of web applications, including non-profit, social networking, social search, pharmaceuticals and social ecommerce He has build successful projects in a variety
of languages, including Java, Ruby, C++, Ruby and Perl
In the primal days of the Web, he wrote a manual helping users connect their
Macintosh to the Web More recently, he reviewed PHP and Scriptaculous Web
Application Interfaces for Packt Publishing.
I'd like to thank my wife for the bliss she brings
Deepak Vohra is a consultant and a principal member of the NuBean.com
software company Deepak is a Sun Certified Java Programmer and Web Component Developer, and has worked in the fields of XML and Java programming and J2EE for
over five years Deepak is the co-author of the Apress book Pro XML Development with Java Technology and was the technical reviewer for the O'Reilly book WebLogic: The Definitive Guide Deepak was also the technical reviewer for the Course Technology PTR book Ruby Programming for the Absolute Beginner, and the technical editor for the Manning Publications book Prototype and Scriptaculous in Action Deepak is also the author of the Packt Publishing books JDBC 4.0 and Oracle JDeveloper for J2EE Development and Processing XML Documents with Oracle JDeveloper 11g.
Trang 8Table of Contents
Chapter 1: Coding Style and Standards 5
Trang 9Coding standard adherence and verification 25
Trang 10Chapter 4: Source Code and Version Control 135
Trang 11Apache with mod_dav_svn 143
Trang 12highlight_string(string str [, bool return]) and highlight_file(string filename [, bool return]) 212
Trang 13Easy to learn and adapt 256
Trang 14[ vii ]
Chapter 8: Deploying Applications 329
Rotating log files and updating symbolic links 334
Trang 15Chapter 9: PHP Application Design with UML 363
Chapter 10: Continuous Integration 395
Trang 16[ ix ]
Trang 18This book will enable you to take your PHP development skills to an enterprise level by teaching the skills and tools necessary to write maintainable and efficient code You will learn how to perform activities such as unit testing, enforcing coding standards, automating deployment, and interactive debugging using tools created for PHP developers—all the information in one place Your code will be more maintainable, efficient, and self-documenting
What this book covers
Chapter 1, Coding Style and Standards, explains how to define a coding standard
that suits your development process and how to enforce it using PHP_CodeSniffer
Chapter 2, Documentation with phpDocumentor, explains how to properly
document your code with phpDocumentor and generate well formatted
developer documentation
Chapter 3, The Eclipse Integrated Development Environment, explains how to install,
customize, and use the free PDT plug-in for Eclipse to create a powerful IDE for PHP development
Chapter 4, Source Code and Version Control, explores the ins and outs of subversion
for distributed version control for development teams It also teaches you to extend subversion functionality with PHP scripts
Chapter 5, Debugging, teaches you to write your own flexible debugging library
and master remote interactive debugging with Xdebug
Chapter 6, PHP Frameworks, explains how to evaluate, compare, and choose
frameworks that suit your projects and development style Master the most
commonly used modules of Zend Framework
Trang 19Chapter 7, Testing, explains testing methods and types, unit testing, creating
comprehensive test suites with PHPUnit, and test-driven development
Chapter 8, Application Deployment, states guidelines for automated and reversible
application deployment, automating upgrades and deployments with Phing
Chapter 9, PHP Application Design with UML, introduces the reader to UML, class
diagrams, sequence diagrams, and use cases
Chapter 10, Continuous Integration, explains how to use CI, keep costs down and
save time by discovering bugs and conflicts in your projects, at an early stage
What you need for this book
To follow along with the examples, you will need a working version of PHP 5
installed on your system Some of the chapters rely on command line tools such as pear and pecl, which are included in the standard distribution of PHP Version 5.2.x
of PHP or higher is recommended for maximum compatibility with the sample code
If you do not already have PHP installed on your system, you can download it from php.net here:
http://www.php.net/downloads.php
Although this book was written and coded on OS X, any flavor of MS Windows
or Linux will do the job as well Basic familiarity with your system's command line and a handy text editor for tweaking the examples will also be helpful
Who this book is for
This book has been written for professional developers new to PHP and experienced PHP developers, who want to take their skills to the next level by learning enterprise level tools and techniques
Conventions
In this book, you will find a number of styles of text that distinguish between
different kinds of information Here are some examples of these styles, and an explanation of their meaning
Code words in text are shown as follows: "We can include other contexts through the use of the include directive."
Trang 20New terms and important words are shown in bold Words that you see on the
screen, in menus or dialog boxes for example, appear in our text like this: "clicking
the Next button moves you to the next screen".
Warnings or important notes appear in a box like this
Tips and tricks appear like this
Reader feedback
Feedback from our readers is always welcome Let us know what you think about this book—what you liked or may have disliked Reader feedback is important for
us to develop titles that you really get the most out of
To send us general feedback, simply drop an email to feedback@packtpub.com, and mention the book title in the subject of your message
If there is a book that you need and would like to see us publish, please send
us a note in the SUGGEST A TITLE form on www.packtpub.com or email
suggest@packtpub.com
If there is a topic that you have expertise in and you are interested in either writing
or contributing to a book, see our author guide on www.packtpub.com/authors
Trang 21Customer support
Now that you are the proud owner of a Packt book, we have a number of things
to help you to get the most from your purchase
Downloading the example code for the book
Visit http://www.packtpub.com/files/code/8389_Code.zip to directly
download the example code
The downloadable files contain instructions on how to use them
Errata
Although we have taken every care to ensure the accuracy of our contents,
mistakes do happen If you find a mistake in one of our books—maybe a mistake
in text or code—we would be grateful if you would report this to us By doing so, you can save other readers from frustration, and help us to improve subsequent versions of this book If you find any errata, please report them by visiting
http://www.packtpub.com/support, selecting your book, clicking on the
let us know link, and entering the details of your errata Once your errata are
verified, your submission will be accepted and the errata added to any list of existing errata Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support
Piracy
Piracy of copyright material on the Internet is an ongoing problem across all media
At Packt, we take the protection of our copyright and licenses very seriously If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy
Please contact us at copyright@packtpub.com with a link to the suspected
Trang 22Coding Style and Standards
A developer's coding style takes time to develop and often reflects his or her
personality Consequently, you are likely to encounter more than just a little resistance when trying to get your team members to adopt a different style However, that is exactly what I will be arguing in this chapter We will learn about the benefits of standardizing on a certain coding style Along the way, we will develop our own style and learn how to automatically enforce that or any standard of your choice using the handy PHP_CodeSniffer utility I am hoping that at the end of this chapter, you will
be able to take a look at your own and your co-workers' coding styles and make the necessary changes to reap the benefits that include code readability
and maintainability
Coding standard considerations
Since you are reading this book, there is a good chance that you have a couple of years
of programming experience under your belt Even if it is in a programming language other than PHP, you have probably had plenty of time to develop your own style of writing code In all likelihood, you have chosen or developed a style that appears to make the most sense to you and that you can adhere to and read easily
However, if you are reading this book, it also means that you are looking to improve your knowledge of PHP Consequently, I assume that you are willing to change your coding style or at least fine-tune it First, let me try to convince you that this is worth the effort
Even if you are writing code only for yourself and have no reasonable expectation that any other programmer will ever look at or work on it, you will probably
want to follow your own coding standards Whether you are aware of it or not,
chances are you are doing this already For example, at some point every programmer decides whether to put the opening brackets on the same line or the one following the if-statement I would also guess that you are doing it the same way every time
Trang 23Personally, I have had many occasions to revisit code that I had written years
ago I can always tell how well defined my coding style was at the time The more consistent the style, the easier it is to get back into the code and understand the intricacies of what it is doing
I think we have all had trouble understanding our own code after not looking at
it for a while If you haven't been in that position already, imagine inheriting a project of substantial size and having to get used to someone else's coding habits and oddities This is where agreeing on a common coding standard pays off If all developers working on the same project agree on the same standard, collaboration becomes so much easier as it takes less time to find and figure things out I'm not just talking about where to place the opening brackets, but rather things such
as locations of classes and libraries, names of methods and attributes, and
inline documentation
Let's consider some of the pros and cons of having formally defined coding
standards—starting with the pros
Pros
It will be easier to understand code Whether you are looking at your own code or that of another development team member, reading and understanding the code will be more fluent This benefit extends not only to current contributors, but also
to programmers who are new to the team or PHP in general Not having to grapple with different styles and conventions will allow them to come up to speed more quickly and allow them to absorb the common style from the beginning
Nowadays, software is often designed, developed, tested, and used in a distributed fashion Team members could be located anywhere in the world With todays tools for communication, the rules for where and how to assemble software teams are being rewritten Just take a look at some of the very successful Open Source projects, many of which have no physical presence at all Consider the Apache Software Foundation or in the PHP space, the Zend Framework project, both of which are very successful examples of highly successful projects with many distributors from all over the globe Projects such as these are some prime candidates for reaping the benefits of a common coding standard
I would go so far as to say that the right coding standard should go beyond style It can improve the quality and robustness of the code For example, having developers consistently validate method parameters will undoubtedly result in a more robust code base