Expert PHP 5 Tools Proven enterprise development tools and best practices for designing, coding, testing, and deploying PHP applications Dirk Merkel PUBLISHING -J BIRMINGHAM - MUMBAI
Preface Chapter 1: Coding Style and Standards Coding standard considerations 5 Pros 6 Cons 7 A PHP coding standard 7 Formatting 8 PHP tags 8 Indenting 8 Line length 8 Line endings 8 Spacing 9 Statements 9 Strings 10 Arrays 11 Control structures 12 Class and method definitions 14 Naming conventions 14 Class names 14 Property and variable names 16 Constant names 17 Method and function names 17 Methodology 19 Type hinting 19 Separating object creation from initialization 19 Class files 20 Class names and directory structures 20 Visibility and access 21 Including source files 22 Comments 23 Inline documentation 23
Coding standard adherence and verification PHP_CodeSniffer for automated checking Installing PHP_CodeSniffer Basic usage Slightly advanced usage Validating against your own coding standard Summary Chapter 2: Documentation with phpdocumentor Code-level documentation Levels of detail Introducing phpdocumentor Installing phpdocumentor DocBlocks Short and long descriptions Tags DocBlock templates Tutorials Naming conventions and how to reference tutorials DocBook syntax Documenting a project Documentation without DocBlocks Documentation with DocBlocks phpdocumentor options Command line reference Config files Browser-based interface Tag reference Standard tags Inline tags PHP4 elements Custom tags Summary Chapter 3: The Eclipse Integrated Development Environment Why Eclipse? Introducing PDT Installing Eclipse Requirements Choosing a package Adding the PDT plugin Basic Eclipse concepts Workspace Views Perspectives 25 25 25 27 27 29 38 39 40 41 42 42 44 44 44 45 46 47 48 49 54 57 67 67 69 70 71 71 83 91 92 92 93 94 96 96 96 98 99 100 101 102 104 r HI
A PDT sample project 106 PDT features 109 Editor 109 Syntax highlighting 109 Code assist 110 Code folding 110 Mark occurrences 111 Override indicators 112 Type, method, and resource navigation 112 Inspection 114 Projects and files 114 PHP explorer 114 Type hierarchy 115 Debugging 115 PDT preferences 119 Appearance 119 Code style 119 Debug 120 Editor 121 New project layout 123 PHP executables 124 PHP interpreter 124 PHP manual 124 PHP servers 125 Templates 125 Other features 126 PHP function reference 126 Eclipse plugins 127 Zend Studio for Eclipse 129 Support 131 Refactoring 131 Code generation 131 PHPUnit testing 132 PhpDocumentor support 133 Zend Framework integration 133 Zend server integration 133 Summary 134 Chapter 4: Source Code and Version Control 135 Common use cases 136 A brief history of source code control 136 CVS 140 Introducing Subversion 142 Client installation 142 Server configuration 143 [ in ]
lane oj contents Apache with mod_dav_svn Subversion concepts Repository Tags Trunk Branches Working (Local) copy Merging Revisions and versions Updating Comparing History/Log Annotating code Reverting Committing Subversion command reference svn svnadmin svnlook svnserve svndumpfilter svnversion Creating a Subversion project Basic version control workflow A closer look at the repository Data store Layout Branching and merging What is a branch? Why branch? How to branch? Maintaining and merging a branch Branching workflow Ul clients Eclipse plug-ins TortoiseSVN WebSVN Subversion conventions and best practices Customizing Subversion Hooks Notifying developers of commits with a post-commit hook Summary 143 143 144 144 144 145 145 146 146 146 146 147 147 148 148 148 148 157 158 158 158 158 159 168 172 172 173 174 175 175 176 177 180 182 182 185 185 189 190 190 194 195 [iv]
Chapter 5: Debugging 197 First line of defense: syntax check 197 Logging 199 Configuration options 200 Customizing and controlling config options: Phplni 202 Phplni example 208 Outputting debug information 209 Functions 209 echo(string $arg1 [, string $... ] / print(string $arg) 210 var_dump(mixed Sexpression [, mixed $expression [, $... ]]) and pnnt_r(mixed $expression [, bool $return= false ]) 210 highlight_string(string str [, bool return]) and highlight_file(string filename [, bool return]) 212 get_class([object object]) 212 get_object_vars(object object) 212 get_class_methods(mixed class_name) 212 get_class_vars(string class_name) 212 debug_backtrace() 213 debug_print_backtrace() 213 exit([string status]) or exit (int status) 213 Magic constants 213 Writing our own debugging class 214 Functional requirements 214 DebugException 215 Introducing Xdebug 232 Installing Xdebug 233 Configuring Xdebug 235 Immediate benefits 237 var_dump() improved 237 var_dump() settings 238 Errors and exceptions beautified 238 Protection from infinite recursion 240 Remote debugging 240 Remote server debug configuration 241 Debugging client configuration 242 Summary 249 Chapter 6: PHP Frameworks 251 Writing your own framework 251 Evaluating and choosing frameworks 252 Community and acceptance 252 Feature road map 253 Documentation 254 Code quality 254 Coding standards and compliance 255 Project fit 255 [v]
Easy to learn and adapt 256 Open source 256 Familiarity 257 Their rules 257 Popular PHP frameworks 257 Zend 258 CakePHP 259 Codelgniter 259 Symfony 260 Yii 260 Zend Framework application 261 Feature list 262 Application skeleton 262 Important concepts 265 Application structure detail 265 Enhancements 269 Adding a layout 269 Adding views 271 Adding logging 274 Adding a database 276 Adding a model 278 Adding a controller 282 Putting it all together 288 Summary 289 Chapter 7: Testing 291 Testing methods 291 Black box 292 White box 292 Gray box 293 Types of testing 294 Unit testing 294 Integration testing 295 Regression testing 296 System testing 296 User acceptance testing 297 Introducing PHPUnit 298 Installing PHPUnit 298 String search project 299 BMH algorithm basics 300 Implementing BMH 301 Unit testing BoyerMooreStringSearch 304 The test class 305 'Assertions 305 [vi]
Organization 306 Our first unit test 307 Extended test class features 309 Automation generating tests from classes 317 Automation: generating classes from tests 321 Test-driven development 321 Code coverage 326 TestCase subclasses 328 Summary 328 Chapter 8: Deploying Applications 329 Goals and requirements 329 Deploying your application 331 Checking out and uploading files 332 Displaying an under-maintenance message 333 Upgrading and installing files 333 Upgrading database schema and data 334 Rotating log files and updating symbolic links 334 Verifying the deployed application 335 Automating deployment 335 Phing 335 Installing Phing 336 Basic syntax and file structure 337 Tasks 338 Targets 339 Properties and property files 340 Types 341 Filters 342 Mappers 342 The project tag 343 Deploying a site 344 Separating external dependencies _ 345 Creating a build script 345 Directory skeleton 349 Subversion export and checkout 350 Building files from templates 352 Maintenance page 353 Database backup 354 Database migrations 356 Going live 358 Putting it all together 358 Backing out 362 Summary 362 [vii]
Chapter 9: PHP Application Design with UML 363 Meta-model versus notation versus our approach 364 Levels of detail and purpose 365 Round-trip and one-way tools 366 Basic types of UML diagrams 367 Diagrams 369 Class diagrams 370 Elements of a class 370 Static methods and properties 372 A class diagram example 372 Relationships 380 Interfaces 382 Example refactored 383 Code generators 383 Sequence diagrams 384 Scope 384 A sequence diagram of the network scanner 384 Use cases 390 Use cases diagrams optional 390 When to create use cases 391 Example use case 391 Summary 394 Chapter 10: Continuous Integration 395 The satellite systems 397 Version control: Subversion 397 Commit frequency 398 Testing: PhpUnit 398 Automation: Phing 399 Coding style: PHP_CodeSniffer 401 Documentation: PhpDocumentor 401 Code coverage' Xdebug 402 Environment setup considerations 402 Do I need a dedicated Cl server? 403 Do I need a Cl tool? 403 Cl tools 403 XINC (Xinc Is Not CruiseControl) 404 phpundercontrol 404 Continuous integration with phpundercontrol 404 Installation 405 Installing CruiseControl 405 Installing phpundercontrol 406 Overlaying CruiseControl with phpundercontrol 407 CruiseControl configuration 409 [ viii ]
Overview of the Cl process and components 409 CruiseControl and project layout 409 Getting the project source 412 Configuring the project: build.xml 413 Configuring CruiseControl 417 Advanced options 420 Running CruiseControl 420 Replacing Ant with Phing 428 Summary 433 Index 435 [ix]