Table Of Contentphp|architect’s
php|architect’s
Guide to
PHP Design Patterns
Guide to
Design patterns are comprehensive, well-tested solutions to common problems
that developers everywhere encounter each day. Although designed for solving
general programming issues, some of them have been successfully adapted to PHP 5 Migration
the specific needs of Web development.
s
n
r
php|architect’s Guide to PHP Design Patterns is the first comprehensive guide e
to the application of design patterns to the PHP development language. t
t
a
Designed to satisfy the need of enterprise-strength development, you will find P Practical porting techniques for
this book both an excellent way to learn about design pattern and an n
irreplaceable reference for your day-to-day programming g the professional developer
i
s
e
With coverage of more than XXX different types of patterns, including BLAH, D
BLAH, BLAH, BLAH and much more, this book is the ideal resource for your P
enterprise development with PHP 4 and PHP 5. H
P
o
t
e
d
i
u
G
s
’
t
c
e
t
i
h
c
r
a
|
p
NanoBooks are excellent, in-depth resources created by the publishers of h
p
php|architect (http://www.phparch.com), the world’s premier magazine dedicated
to PHP professionals.
NanoBooks focus on delivering high-quality content with in-depth analysis and
expertise, centered around a single, well-defined topic and without any of the fluff
of larger, more expensive books.
t
USA $21.99 a
From the publishers of e
Canada $29.99 w
S
U.K. £16.99 Net E.
n
o Stefan Priebsch
Shelve under PHP/Web Development/Internet Programming s
a
J
Licensed to:
7.50 x 9 .25 7.50 x 9 .25
Blake Hartle
.309
[email protected]
User #60389
php|architect’s
PHP 5 Migration Guide
byStefanPriebsch
php|architect’sPHP5MigrationGuide
ContentsCopyright©2007-2008StefanPriebsch–AllRightsReserved
Bookandcoverlayout,designandtextCopyright©2004-2008MarcoTabini&Associates,Inc.–AllRightsReserved
FirstEdition:June2008
ISBN:978-0-9738621-9-5
ProducedinCanada
PrintedintheUnitedStates
Nopartofthisbookmaybereproduced,storedinaretrievalsystem,ortransmittedinanyformor
bymeanswithoutthepriorwrittenpermissionofthepublisher,excetinthecaseofbriefquotations
embeddedincriticalreviewsorarticles.
Disclaimer
Althougheveryefforthasbeenmadeinthepreparationofthisbooktoensuretheaccuracyofthe
informationcontainedtherein, thisbookisprovided”as-is”andthepublisher, theauthor(s), their
>
distributorsandretailers,aswellasallaffiliated,relatedorsubsidiarypartiestakenoresponsibility m
o
foranyinaccuracyandanyandalldamagescaused,eitherdirectlyorindirectly,bytheuseofsuch k.c
information. Wehaveendeavouredtoproperlyprovidetrademarkinformationonallcompaniesand oo
b
productsmentionedinthebookbytheappropriateuseofcapitals.However,wecannotguaranteethe e
w
accuracyofsuchinformation. o
w
w.
w
MarcoTabini&Associates,TheMTAlogo,php|architect,thephp|architectlogo,NanoBookandthe w
<
NanoBooklogoaretrademarksorregisteredtrademarksofMarcoTabini&Associates,Inc. k
o
o
B
e
w!
Writtenby StefanPriebsch o
W
Publishedby MarcoTabini&Associates,Inc. m
28BombayAve. o
Toronto,ONM3H1B7 d fr
Canada oa
nl
w
(416)630-6202/(877)630-6202 o
D
[email protected]/www.phparch.com
Publisher MarcoTabini
LayoutandDesign ArbiArzoumani
ManagingEditor ElizabethNaramore
FinanceandResourceManagement EmanuelaCorso
>
m
o
c
k.
o
o
b
e
w
o
w
w.
w
w
<
k
o
o
B
e
w!
o
W
m
o
d fr
a
o
nl
w
o
D
>
m
o
c
k.
o
o
b
e
w
o
w
w.
w
w
<
k
o
o
B
e
w!
o
W
m
o
d fr
a
o
nl
w
o
D
Contents
Chapter1—Introduction 3
AShortHistoryof(Internet)Time . . . . . . . . . . . . . . . . . . . . . . . . . 3
TheBirthofPHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 >
m
PHP5andtheBigMigration . . . . . . . . . . . . . . . . . . . . . . . . . 8 co
k.
ALookAhead:PHP6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 o
o
b
e
w
Chapter2—Strategies 13 o
w
MigrationStrategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 w.
w
w
NeverTouchaRunningSystem . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 <
k
SystemEnvironment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 o
o
B
ProgramCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 e
w!
AlwaysUsetheLatestVersion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 o
W
StartingfromScratch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 m
o
RewritingProgramCodefromScratch . . . . . . . . . . . . . . . . . . . . 22 d fr
RebuildingtheSystemEnvironmentfromScratch. . . . . . . . . . . . . 24 oa
nl
StrikingaBalance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 w
o
D
Chapter3—MigrationAspects 29
ImportantAspectsofMigration. . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Platform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Processor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
InstructionSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
WordLength . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
vi ” CONTENTS
ByteOrder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Mountingformsandinterfaces . . . . . . . . . . . . . . . . . . . . . . . . 37
Operatingsystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
WordLength . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
LineEndings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
AccessRights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
PathsandFileNames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
TemporaryFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
TheSearchPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
CharacterSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
SQLDoesNotEqualSQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
ProgramCodeintheDatabase . . . . . . . . . . . . . . . . . . . . . . . . 55
>
DataTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 m
o
c
CharacterSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 k.
o
o
BackupandRestore. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 b
e
w
WebServer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 o
w
ApacheandApache2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 w.
w
Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 w
<
CompilingApache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 ok
o
MultipleWebServersonOneSystem . . . . . . . . . . . . . . . . . . . . 65 B
e
PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 w!
o
W
WebServerIntegration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 m
CompilingPHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 d fro
ThreadModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 a
o
PHPConfiguration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 wnl
o
PHPExtensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 D
InstallingMultiplePHPVersions . . . . . . . . . . . . . . . . . . . . . . . 98
PHPCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
ThirdpartyPHPcode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
YourOwnPHPCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
ExternalPrograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
InterfacestoThird-PartySystems . . . . . . . . . . . . . . . . . . . . . . . . . 105
CharacterEncodings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
CONTENTS ” vii
Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Chapter4—PreparingtheMigration 117
StepstoPreparingtheMigration . . . . . . . . . . . . . . . . . . . . . . . . . . 117
TheExistingApplicationandEnvironment . . . . . . . . . . . . . . . . . . . . 118
TheTargetSystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
PlanningtheMigration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Chapter5—TheMigration 129
Preparations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
TheFirstTestSystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
TheSecondTestSystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 >
m
FindingRelevantTestCases . . . . . . . . . . . . . . . . . . . . . . . . . . 133 o
c
k.
CreatingTestData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 o
o
b
CreatingTests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 e
w
o
Refactoring. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 w
w.
EliminateRedundantCode . . . . . . . . . . . . . . . . . . . . . . . . . . 138 w
w
ShortenCodeBlocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 <
k
o
SeparateDifferentConcerns . . . . . . . . . . . . . . . . . . . . . . . . . 140 o
B
Migrating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 w! e
FixingExistingBugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 o
W
ReplacingModules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 m
o
FixingSyntaxErrors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 d fr
a
FixingAllPHPErrorMessages . . . . . . . . . . . . . . . . . . . . . . . . 145 o
nl
w
FixingLogicalErrors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 o
D
NormalizingthePHPConfiguration . . . . . . . . . . . . . . . . . . . . . 147
MigratingtheProductionSystem . . . . . . . . . . . . . . . . . . . . . . . . . 148
FinishingtheMigration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Chapter6—AftertheMigration 153
What’sNext? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
ModularProgramming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
EstablishCodingGuidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
viii ” CONTENTS
DefensiveCoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
DoNotBeTheFirst . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
ContinuousRefactoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
AgileMigration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Chapter7—Tools 159
ToolsMakeAlltheDifference . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
VersionControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
CommandLineTools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
SendingHTTPRequestsandDownloadingFiles . . . . . . . . . . . . . . 160
SearchFilesandDirectories . . . . . . . . . . . . . . . . . . . . . . . . . . 161
ReplacinginFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
ComparingFilesandDirectories . . . . . . . . . . . . . . . . . . . . . . . 162
Validating(X)HTMLFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 m>
o
TheW3CValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 c
k.
o
HTMLTidy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 o
b
e
TheTidyPHPextension . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 w
o
ValidatingCSSFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 w
w.
ValidatingXMLFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 w
w
<
xmllint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 k
o
PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Bo
e
StaticAnalysisofJavaScriptFiles . . . . . . . . . . . . . . . . . . . . . . . . . . 173 w!
o
jsl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 W
m
JSLint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 o
FirefoxExtensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 d fr
a
o
Webdeveloper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 nl
w
Firebug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Do
PHP’sOwnMeans. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
ThePHPConfiguration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
SyntaxCheck . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
PrependandAppendFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
PEARComponents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
PHP_Compat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
PHP_Beautifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
CONTENTS ” ix
PHP_CodeSniffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
PHP_CompatInfo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
VirtualMachines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
VMWare . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
InstallingAVirtualMachine . . . . . . . . . . . . . . . . . . . . . . . . . . 195
WorkingWithSnapshots . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
TestTools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
UnitTestswithPHPUnit . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
SystemTestswithSelenium . . . . . . . . . . . . . . . . . . . . . . . . . . 202
ProgramAnalysisandDebugging . . . . . . . . . . . . . . . . . . . . . . . . . 209
Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
UsefulFeatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Tracing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
>
Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 m
o
c
CodeCoverage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 k.
o
o
BuildAutomation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 b
e
w
Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 o
w
CodeQualityAssurance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 w.
w
TestAutomation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 w
<
CodeCoverageStatistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 ok
o
B
e
Chapter8—MigratingPHPCode 225 w!
o
ChapterOverview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 W
m
CaseSensitivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 o
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 d fr
a
o
Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 nl
w
MagicConstants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Do
FunctionsandMethods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
NameConflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
ReservedKeywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Classes,InterfacesandExceptions . . . . . . . . . . . . . . . . . . . . . . 239
Description:This is the only book you will need to help you through the rough spots when migrating your PHP apps from PHP4 to PHP5. Not only is this an extremely comprehensive and in-depth resource, it will show you virtually every pitfall you may encounter and will undoubtedly make your migration as smooth as