e d i u g t c e t i h c r a | p h p a PHP Playbook Brandon Savage The PHP Playbook A php|architect Guide byBrandonSavage ThePHPPlaybook ContentsCopyright©2010–2011BrandonSavage–AllRightsReserved Bookandcoverlayout,designandtextCopyright©2004-2011BlueParabola,LLC.anditspredecessors–AllRights Reserved FirstEdition:October2011 ISBN:978-0-98-103454-6 ProducedinCanada PrintedintheUnitedStates Nopartofthisbookmaybereproduced,storedinaretrievalsystem,ortransmittedinanyformor bymeanswithoutthepriorwrittenpermissionofthepublisher,exceptinthecaseofbriefquotations embeddedincriticalreviewsorarticles. Disclaimer Althougheveryefforthasbeenmadeinthepreparationofthisbooktoensuretheaccuracyofthe informationcontainedtherein, thisbookisprovided”as-is”andthepublisher, theauthor(s), their distributorsandretailers,aswellasallaffiliated,relatedorsubsidiarypartiestakenoresponsibility foranyinaccuracyandanyandalldamagescaused,eitherdirectlyorindirectly,bytheuseofsuch information. Wehaveendeavouredtoproperlyprovidetrademarkinformationonallcompaniesand productsmentionedinthebookbytheappropriateuseofcapitals.However,wecannotguaranteethe accuracyofsuchinformation. Blue Parabola, The Blue Parabola logo, php|architect, the php|architect logo, NanoBook and the NanoBooklogoaretrademarksorregisteredtrademarksofBlueParabola,LLC,itsassigns,partners, predecessorsandsuccessors. Writtenby BrandonSavage Publishedby BlueParabola,LLC. 28BombayAve. Toronto,ONM3H1B7 Canada (416)630-6202/(877)630-6202 [email protected]/www.phparch.com Publisher MarcoTabini TechnicalReviewer SimonHarris CopyEditor LoriAnnPannier LayoutandDesign ArbiArzoumani ManagingEditor ElizabethTuckerLong FinanceandResourceManagement EmanuelaCorso Contents Acknowledgments ix Introduction xi Chapter1—DebuggingPHPProjects 1 IntroductiontoDebugging . . . . . . . . . . . . . . . . . . . . . . . . . . 1 IntroductiontoXdebug . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 InstallingXdebug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 XdebugConfigurationOptions . . . . . . . . . . . . . . . . . . . . . . . . 3 UsingFunctionTraces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 OutputtingVariables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 HandlingErrorswithPHP . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 FindingandSquashingBugs . . . . . . . . . . . . . . . . . . . . . . . . . 11 Chapter2—Test-DrivenDevelopment 15 PHPUnitQuickandDirty . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 HowtoTestWhenTimeisNotAllottedforIt . . . . . . . . . . . . . . . . 18 ConvincingaManagerthatUnitTestingMatters. . . . . . . . . . . . . . 20 KnowingWhenNottoUseUnitTesting . . . . . . . . . . . . . . . . . . . 24 Chapter3—ApplicationOptimization 27 TheOneThingYouMustDoBeforeOptimizing . . . . . . . . . . . . . . 27 OptimizingDatabaseQueries . . . . . . . . . . . . . . . . . . . . . . . . . 29 FunctionCallsInLoops . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 SpottingCodeInefficiencies. . . . . . . . . . . . . . . . . . . . . . . . . . 33 vi ” CONTENTS OptimizationstoAvoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 WhenNottoOptimize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Chapter4—ImprovingPerformance 41 AddingOpcodeCaching . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 AddingMemcache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 AddingDatabaseServers. . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 AddingWebServers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 TheBenefitsofExpandingHardware . . . . . . . . . . . . . . . . . . . . 44 TheDrawbacksofExpandingHardware . . . . . . . . . . . . . . . . . . . 45 Chapter5—CachingTechniques 47 WhatisCaching? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 RulesforCaching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 File-BasedCaches. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Memory-basedCaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 AlternativePHPCache(APC) . . . . . . . . . . . . . . . . . . . . . . . . . 54 Memcached . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 AvoidingthePitfallsofCaching . . . . . . . . . . . . . . . . . . . . . . . . 56 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Chapter6—HarnessingVersionControl 59 WhatIsVersionControl? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 WhyDoesVersionControlMatter? . . . . . . . . . . . . . . . . . . . . . . 60 SellingAManageronVersionControl . . . . . . . . . . . . . . . . . . . . 61 WhichVersionControlShouldYouUse?. . . . . . . . . . . . . . . . . . . 62 EssentialSubversionSyntax . . . . . . . . . . . . . . . . . . . . . . . . . . 64 EssentialGitSyntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 RulesofVersionControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 GuerrillaVersionControl . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Chapter7—RefactoringStrategies 85 IntroductiontoRefactoring . . . . . . . . . . . . . . . . . . . . . . . . . . 85 WhyRefactor? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 ThingsDevelopersMustDoBeforeRefactoring . . . . . . . . . . . . . . 87 CONTENTS ” vii HowtoRefactor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 DevelopingaCodingStandard . . . . . . . . . . . . . . . . . . . . . . . . 90 RefactoringforTestability . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 RefactoringforAbstraction . . . . . . . . . . . . . . . . . . . . . . . . . . 103 RefactoringforLogic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Chapter8—WorstPractices 119 ThinkingSecurityIsforWhenanApplicationisFinished. . . . . . . . . 119 SpendingTooMuchTimeCoding,NotEnoughTimeDesigning. . . . . 121 CatchingNIHSyndrome . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 TryingtoMicroOptimize . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 NotDevelopingwithStrictnessOperators . . . . . . . . . . . . . . . . . 126 NotDevelopingwithaStyleGuide . . . . . . . . . . . . . . . . . . . . . . 126 Chapter9—BecomingaHappyDeveloper 131 TheImportanceofQuiet . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 SomeSpecsonSpecDevelopment . . . . . . . . . . . . . . . . . . . . . . 132 EffectiveIssueTracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 EffectiveProjectManagement . . . . . . . . . . . . . . . . . . . . . . . . 134 PickingtheRightCompanytoWorkFor . . . . . . . . . . . . . . . . . . . 135 Acknowledgments Thankstomymotherforherloveandsupport,andthebeliefthatIcanaccomplish thatwhichIsetmymindtodoing. Thanksalsotomylovingwife,Debbie,whoenduredasIwroteandrewrotevarious partsofthisbook. For their mentorship and involvement, special thanks belong to Keith Casey, Eli White,CalEvans,MatthewTurlandandmanyothers. ThankyoutoTravisSwicegoodforhisreviewandcontributiontotheVersionCon- trolchapter:itisbetterforhisrecommendations. ThankstoElizabethTuckerLongforherpatiencewhilethisbookwaswrittenand fortheexceptionaljobeditingandmanagingtheproject,fromconceptiontocom- pletion.