Table Of Contentphp|architect’s
php|architect’s
Zend PHP 5 Certification
Guide to
Study Guide
Zend's new PHP 5 Certification Exam represent an excellent tool for professional k
PHP developers who want to distinguish themselves in their field. or Programming with
w
e
php|architect's Zend PHP 5 Certification Study Guide, edited and produced by m
the publishers of php|architect magazine, provides the most comprehensive and a
r
thorough preparation tool for developers who wish to take the exam. F
d
n ZEND FRAMEWORK
This book provides complete coverage of every topic that is part of the exam, e
Z
including:
h
t
i
W
✔ PHP Basics g
✔ Functions n
i
m
✔ Arrays
m
✔ Strings and Patterns
a
✔ Web Programming r
g
✔ Object Oriented Programming
o
✔ Database Programming r
P
✔ Object-oriented Design o
✔ XML and Web Services t
e
✔ Security d
✔ Streams and Network Programming ui
G
✔ Differences Between PHP 4 and 5
s
’
t
c
e
NEW IN THE SECOND EDITION: advanced database topics (PDO/mysqli), errata, it
h
new examples, and much, much more! c
r
a
|
p
h
p
MSRP $32.99 USD
From the publishers of
s
n
a
v
E
al Cal Evans
Shelve under PHP/Web Development/Internet Programming C
7.50 x 9.25 7.50 x 9.25
.571
Licensed to:
Wei Dai
neriodavid@gmail.com
User #39728
php|architect’s Guide to
Programming with
Zend Framework
byCalEvans
php|architect’sGuidetoProgrammingwithZendFramework
ContentsCopyright©2007-2008CalvinEvans–AllRightsReserved
Bookandcoverlayout,designandtextCopyright©2004-2008MarcoTabini&Associates,Inc.–AllRightsReserved
FirstEdition:January2008
ISBN:978-0-9738621-5-7
ProducedinCanada
PrintedintheUnitedStates
Nopartofthisbookmaybereproduced,storedinaretrievalsystem,ortransmittedinanyformor
bymeanswithoutthepriorwrittenpermissionofthepublisher,excetinthecaseofbriefquotations
embeddedincriticalreviewsorarticles.
Disclaimer
Althougheveryefforthasbeenmadeinthepreparationofthisbooktoensuretheaccuracyofthe
informationcontainedtherein,thisbookisprovided“as-is”andthepublisher,theauthor(s),theirdis-
tributorsandretailers,aswellasallaffiliated,relatedorsubsidiarypartiestakenoresponsibilityforany
inaccuracyandanyandalldamagescaused,eitherdirectlyorindirectly,bytheuseofsuchinforma-
tion.Wehaveendeavouredtoproperlyprovidetrademarkinformationonallcompaniesandproducts
mentionedinthebookbytheappropriateuseofcapitals.However,wecannotguaranteetheaccuracy
ofsuchinformation.
MarcoTabini&Associates,TheMTAlogo,php|architect,thephp|architectlogo,NanoBookandthe
NanoBooklogoaretrademarksorregisteredtrademarksofMarcoTabini&Associates,Inc.
Writtenby CalEvans
Publishedby MarcoTabini&Associates,Inc.
28BombayAve.
Toronto,ONM3H1B7
Canada
(416)630-6202/(877)630-6202
info@phparch.com/www.phparch.com
Publisher MarcoTabini
TechnicalReviewer MatthewWeierO’Phinney
LayoutandDesign ArbiArzoumani
ManagingEditor ElizabethNaramore
FinanceandResourceManagement EmanuelaCorso
Dedications
Iwouldliketodedicatethisbooktothefollowingpeople,withoutwhom,itwouldnot
havehappened:
• Tomymother—forinstillinginmymyloveofwriting.
• Tomywife, thelovelyandtalentedKathy—whoIlovedearlybecausesheputs
upwithme.
• Tomykids,BeckyandJ.C.—whoIloveanadore,evenwhenI’mignoringthem
towrite.
• ToMarco,Elizabeth,PaulandSean—forfriendship.
• ToMarkdeVisser—forthegreatestjobI’veeverhad.
• ToMatthewWeierO’Phinney—forbeingnicewhenyoucouldhavebeenmean.
• ToMr.JimmyBuffet—Youdon’tknowmebutIcouldnothavefinishedthisbook
withoutyourmusic.MyJimmyBuffetplaylistismybankofbadhabits.
Contents
Foreword xiii
Chapter1—WhatmakestheFrame-work 1
WhyUseaFramework? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
WhichFrameworkisRightforMe? . . . . . . . . . . . . . . . . . . . . . . 1
WhyZendFramework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
MVCinaNutshell. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
IntroducingZendFramework . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
ABriefHistoryofZendFramework . . . . . . . . . . . . . . . . . . . . . 4
TheZendFrameworkCommunity . . . . . . . . . . . . . . . . . . . . . . 5
TheZendFrameworkLicenseandIntellectualPropertyConcerns . . . 5
WhatYouNeedToGoFromHere . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Chapter2—GettingStarted 9
BuildingYourFirstApp. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Step1:DownloadacopyofZendFramework . . . . . . . . . . . . . . . 10
Step2:CreateYourDirectoryStructure . . . . . . . . . . . . . . . . . . . 11
Step3:CreateYourBootstrapFile . . . . . . . . . . . . . . . . . . . . . . 12
Step4:CreateYour.htaccessFile . . . . . . . . . . . . . . . . . . . . . . . 15
Step5:CreateYourController . . . . . . . . . . . . . . . . . . . . . . . . . 16
Step6:FireUpaBrowserandRevelInYourHandiwork . . . . . . . . . 17
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
viii ” CONTENTS
Chapter3—TheController 21
LayingtheGroundworkforaSampleApplication . . . . . . . . . . . . . . . . 21
CreatingaSampleApplication . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
BaseController. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
HelpersandPlugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
PlacingtheHelper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
UsingtheHelper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Chapter4—TheModel 41
TypesofModelImplementation . . . . . . . . . . . . . . . . . . . . . . . . . . 41
NoModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
LightModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
HeavyModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
AddingRegistration&Login. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
CreatingandConnectingtotheDatabase. . . . . . . . . . . . . . . . . . 42
TheMemberClass-RegisteringNewMembers . . . . . . . . . . . . . . . 44
AllowingMemberstoLogin . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Chapter5—TheView 59
InstantiatingtheViewandRenderingOutput . . . . . . . . . . . . . . . . . . 59
ViewScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
EscapingOutput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
ViewHelpers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Chapter6—DataAccess 73
ConnectingtotheDatabase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
FetchingData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
fetchAll() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
fetchAssoc() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
fetchCol(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
fetchPairs() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
fetchRow() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
CONTENTS ” ix
fetchOne() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Profiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Chapter7—Authentication 87
AboutZend_Auth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
UsingZend_Auth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Chapter8—SuperSecretNinjaClass:Globals.php 99
SettingUpGlobals.php. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
UsingGlobals.phpwithZend_Cache . . . . . . . . . . . . . . . . . . . . . . . 103
StoringGlobalConfigurationValues . . . . . . . . . . . . . . . . . . . . . . . . 113
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Chapter9—WebServices 121
IntroductiontoFlickr’sAPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
IntegratingYahoo!andFlickrAPIs . . . . . . . . . . . . . . . . . . . . . . . . . 123
CreatingYourOwnWebService . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Chapter10—Exceptions 137
Exceptions:APrimer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Chapter11—RichInternetApplications 155
MakingourSampleAppIntoanRIA . . . . . . . . . . . . . . . . . . . . . . . . 156
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Chapter12—ZendFrameworkPartyTricks 167
CleaningYourCacheThroughCLI . . . . . . . . . . . . . . . . . . . . . . . . . 167
SettinguptheBootstrap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
CreatingaNewBootstrap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
ProcessController.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Description:The Zend Framework License and Intellectual Property Concerns . other two parts of the initiative are Zend's Developer. Zone and an Eclipse based IDE for PHP.) The first public release was on March 4,. 2006, version 0.1.2. Because the community is comprised of both beginner and advanced pro-.