Aug 7, 2009
“PHP as an Object Oriented Programming language”WorkShop
Dear All ,
We will be having a workshop Insh’Allah next Sunday 9th August 2009 , applying the object oriented programming concepts for PHP .
This session requires that at least you have knowledge with PHP.
Place : 612 or 512 Hall. updated: TEMPUS labs , 4th floor , power mechanics block , – Faculty of Engineering – Mansoura University.
Time : 10:30 AM to 2:30 PM.
Workshop Contents :
1- Basics of OOP .
2- OO features new to PHP5.
3- Building Some real-world classes.
4- Database Classes.
5- Inheritance .
This workshop is expected to be extended to a second part , that will contain:
- Advanced OOP concepts.
- Using PHP Data Objects (PDO).
It’s recommended to bring your laptops , installed with any IDE that supports PHP !
See you there !
What Does This Book Have to Offer? …………………………………………………………….. xvi
Who Should Read This Book? ………………………………………………………………………. xvi
Requirements …………………………………………………………………………………………… xvi
Software …………………………………………………………………………………….. xvi
Skills …………………………………………………………………………………………..xvii
Overview of Contents …………………………………………………………………………………xvii
Companion Website …………………………………………………………………………………. xix
Resources ………………………………………………………………………………………………… xx
Websites ……………………………………………………………………………………… xx
Books …………………………………………………………………………………………. xx
1
W HA T A T A N G L E D W E B W E W E A V E 1
Do We Really Need Objects? ………………………………………………………………………… 2
Just a Scripting Language ………………………………………………………………….. 2
Object Orientation Is for Large Software Shops ……………………………………….. 3
Leave Well Enough Alone …………………………………………………………………. 3
Increased Complexity ………………………………………………………………………. 3
The PHP Culture …………………………………………………………………………………………. 4
2
B A S IC S O F O B J E CT -O R IE NT E D PR O G R AM M IN G 5
Class ………………………………………………………………………………………………………. 6
Classes Versus Records …………………………………………………………………….. 6
A Cohesive Whole ………………………………………………………………………….. 6
Objects Are Instances ………………………………………………………………………. 6
Objects Need Access Modifiers ……………………………………………………………………… 7
Object Reuse and Inheritance ………………………………………………………………………… 7
Multiple Inheritance …………………………………………………………………………. 8
Having Your Cake and Eating It Too …………………………………………………….. 8
Where to Go from Here ……………………………………………………………………………….. 9
3
O B J E C T -O R I E N T E D F E AT U R E S N E W T O P H P 5 11
Access Modifiers ………………………………………………………………………………………. 12
Built-in Classes …………………………………………………………………………………………. 12
Exceptions …………………………………………………………………………………… 12
Database Classes ………………………………………………………………………….. 13
Web Services ………………………………………………………………………………. 13
Reflection Classes …………………………………………………………………………. 14
Iterator ……………………………………………………………………………………….. 14
Backward Compatibility ……………………………………………………………………………… 14
Pass By Reference …………………………………………………………………………. 14
Prognosis ……………………………………………………………………………………. 15
Where to Go from Here ……………………………………………………………………………… 15
Adoption of PHP 5 ………………………………………………………………………… 16
Compromise ………………………………………………………………………………… 16
4
S HO W A LI TT L E C L AS S 17
Design …………………………………………………………………………………………………… 18
Defining the Problem ……………………………………………………………………… 18
Not the Da Vinci Code ………………………………………………………………………………. 19
The Constructor …………………………………………………………………………….. 19
Referencing Instance Variables …………………………………………………………. 20
Wrapper Methods …………………………………………………………………………. 20
Creating an Instance …………………………………………………………………………………. 21
What Have You Accomplished? ……………………………………………………………………. 23
But Will It Fly? …………………………………………………………………………………………. 23
5
M O D U R CL A SS 25
Upgrading to PHP 5 ………………………………………………………………………………….. 26
Access Modifiers …………………………………………………………………………… 26
The Constructor …………………………………………………………………………….. 28
Modifying Your Class ………………………………………………………………………………… 29
Reconstructing the Constructor ………………………………………………………….. 29
Filtering Content ……………………………………………………………………………. 31
Resetting the Array ………………………………………………………………………… 32
Summary of Changes ………………………………………………………………………………… 33
6
T H E T HU M B N A I LI M A G E CL A SS 35
What Does a Designer Do? …………………………………………………………………………. 36
Mimicking the Designer ………………………………………………………………….. 36
Help from PHP Functions …………………………………………………………………. 36
The ThumbnailImage Class ………………………………………………………………………….. 37
Data Members ……………………………………………………………………………… 37
Deconstructing the Constructor ………………………………………………………….. 37
Two Ways to Construct an Object ……………………………………………………… 38
Internal Behavior—Private Methods ……………………………………………………. 39
Must It Be Private? …………………………………………………………………………. 40
A Helper Method ………………………………………………………………………….. 40
Public Methods …………………………………………………………………………….. 41
Garbage Collection ……………………………………………………………………….. 41
viii C on t en t s in D et ai l
Displaying the Image ……………………………………………………………………… 41
Get and Set Methods ……………………………………………………………………… 42
Image Quality ………………………………………………………………………………. 42
When to Change the Quality ……………………………………………………………. 43
Displaying a Thumbnail ……………………………………………………………………………… 44
Putting It All Together …………………………………………………………………………………. 44
Where to Go from Here ……………………………………………………………………………… 45
7
B U IL D IN G T H E P A G E N A VI G A T O R C L A SS 47
How Will the Navigator Behave? ………………………………………………………………….. 47
Different Kinds of Searches ………………………………………………………………. 48
What Will It Look Like? ………………………………………………………………………………. 48
The Code ……………………………………………………………………………………………….. 49
The Constructor …………………………………………………………………………….. 51
Ain’t Misbehavin’ ………………………………………………………………………….. 52
Other Constructor Method Calls ………………………………………………………… 52
The getNavigator Method …………………………………………………………………………… 54
Move First and Move Previous ………………………………………………………….. 54
Main Body of the Navigator …………………………………………………………….. 55
Move Next and Move Last ………………………………………………………………. 56
Current and Total Number of Pages …………………………………………………… 56
Where to Go from Here ……………………………………………………………………………… 56
8
U S I N G T HE P AG E NA V I G A T O R C L AS S 57
DirectoryItems Change ……………………………………………………………………………….. 58
CSS and Reusability ………………………………………………………………………………….. 58
Paging with Class …………………………………………………………………………………….. 60
Displaying an Array Slice ………………………………………………………………… 61
Creating the PageNavigator Object …………………………………………………… 62
Where to Go from Here ……………………………………………………………………………… 63
9
D A T A B A S E C L A SS E S 65
Using What You Know ………………………………………………………………………………. 65
One Lump or Two? ……………………………………………………………………………………. 66
The MySQLConnect Class …………………………………………………………………………… 66
A Class-Conscious Variable ……………………………………………………………… 67
Making Other Connections ………………………………………………………………. 68
You Can Only Get There from Here ……………………………………………………. 68
The MySQLResultSet Class …………………………………………………………………………… 69
Using the Page Navigator …………………………………………………………………………… 70
Ordering, Filtering, and Extracting …………………………………………………….. 71
Traversing the Result Set ………………………………………………………………….. 72
Your Navigator Needs Directions ………………………………………………………. 73
Where to Go After the Navigator ………………………………………………………………….. 74
ix
C on t en ts in D et ail
10
I M PR O V E M E N T T H RO U G H I N HE R IT AN C E 75
The Standard PHP Library ……………………………………………………………………………. 76
Extending a Class Through Inheritance ……………………………………………………………. 76
The Exception Class ……………………………………………………………………….. 77
protected ……………………………………………………………………………………. 77
final …………………………………………………………………………………………… 78
More Magic Methods …………………………………………………………………….. 78
Replacing Errors with Exceptions …………………………………………………………………… 79
The MySQLException Class …………………………………………………………………………. 80
Changes to the MySQLConnect Class …………………………………………………………….. 81
Prodding Your Class into Action ………………………………………………………… 82
Catching Exceptions ………………………………………………………………………………….. 83
Implementing an Interface …………………………………………………………………………… 84
Learning About the Iterator Interface …………………………………………………… 85
Implementation …………………………………………………………………………….. 86
Leaving a Method Undefined ……………………………………………………………. 88
Implementation and Access ……………………………………………………………… 88
Iterating Through a MySQLResultSet …………………………………………………… 89
Where to Go from Here ……………………………………………………………………………… 89
11
A D V A NC E D O B JE C T – O RI E N T E D P RO G RA M M I N G
C O NC E P T S 91
Abstract Classes ……………………………………………………………………………………….. 91
Private Methods Can’t Be Abstract …………………………………………………….. 92
Interface or Pure Abstract Class? ……………………………………………………….. 92
Polymorphism ………………………………………………………………………………………….. 93
Controlling How Functions Are Used ………………………………………………….. 93
Static Classes ………………………………………………………………………………………….. 94
Static Math Classes ……………………………………………………………………….. 94
Instances of Static Classes ……………………………………………………………….. 95
Preventing Instantiation of a Static Class ………………………………………………. 96
Design Patterns ………………………………………………………………………………………… 96
The Singleton Pattern ……………………………………………………………………… 96
Which Implementation? ………………………………………………………………….. 98
Where to Go from Here ……………………………………………………………………………… 98
12
K E E PI N G IT F R E S H 99
SimpleXML ……………………………………………………………………………………………. 100
XML …………………………………………………………………………………………. 100
RSS …………………………………………………………………………………………. 101
Structure of an RSS File …………………………………………………………………. 101
Reading the Feed ………………………………………………………………………… 102
Site-Specific Search …………………………………………………………………………………. 103
Google API ……………………………………………………………………………….. 104
AJAX ……………………………………………………………………………………….. 104
Installing SOAP …………………………………………………………………………… 104
x C on t en ts in D et ai l
The SOAP Extension …………………………………………………………………….. 105
A SOAP Client ……………………………………………………………………………. 105
Testing the Functionality ………………………………………………………………… 108
Viewing the Results Using AJAX ………………………………………………………. 109
Complex Tasks Made Easy ………………………………………………………………………… 110
Would You Want to Do It Procedurally? ………………………………………………………… 110
13
MORE MAGIC METHODS 111
__get and __set ………………………………………………………………………………………. 112
Is It Worth It? ……………………………………………………………………………… 113
__isset and __unset ………………………………………………………………………………….. 113
__call ………………………………………………………………………………………………….. 114
__autoload ……………………………………………………………………………………………. 115
__sleep and __wakeup …………………………………………………………………………….. 116
__clone ………………………………………………………………………………………………… 116
Where’s Waldo? ………………………………………………………………………… 117
clone ……………………………………………………………………………………….. 118
Aggregate Classes ………………………………………………………………………. 119
A Get Method for Object Data Members of an Aggregate Class ……………… 121
No Clones Allowed ……………………………………………………………………… 122
A Note About Overloading ……………………………………………………………………….. 122
14
C R E A T I N G D O C UM E N T AT IO N US I N G
T H E R E F LE C T IO N CL A S SE S 125
What Are the Reflection Classes? ………………………………………………………………… 126
The Reflection Group of Classes ………………………………………………………………….. 126
The Reflection Class ……………………………………………………………………… 127
The ReflectionClass Class ………………………………………………………………. 128
ReflectionMethod and ReflectionParameter …………………………………………. 129
Built-in Functions ………………………………………………………………………….. 129
What Format Do You Want? ……………………………………………………………………… 130
The Documenter Class ……………………………………………………………………………… 130
Describing the Documenter Class …………………………………………………….. 130
Describing Methods and Data Members ……………………………………………. 131
The Constructor …………………………………………………………………………… 132
Method and Data Member Modifiers ………………………………………………… 132
Using the Documenter Class ………………………………………………………………………. 134
Creating a Sidebar of Classes and Interfaces ……………………………………… 134
Formatting Detailed Documentation ………………………………………………….. 134
Formatting Comments for the Documenter …………………………………………… 136
Reflecting ……………………………………………………………………………………………… 137
15
E X T E N D IN G S Q L IT E 139
Brief Overview ……………………………………………………………………………………….. 140
Directory Structure …………………………………………………………………………………… 140
xi
C on t en ts in D et ail
How It’s Done ………………………………………………………………………………………… 141
Getting Started ………………………………………………………………………………………. 141
Creating a Table …………………………………………………………………………………….. 142
Views ………………………………………………………………………………………………….. 143
Triggers ……………………………………………………………………………………………….. 144
PHP Implementation of SQLite …………………………………………………………………….. 145
Extending SQLiteDatabase ………………………………………………………………………… 145
Override the Query Methods ……………………………………………………………………… 146
Error Messages …………………………………………………………………………… 147
Query Methods …………………………………………………………………………… 148
Utility Methods ……………………………………………………………………………………….. 151
Getting Metadata ………………………………………………………………………… 152
Using Metadata ………………………………………………………………………….. 153
User-Defined Functions ……………………………………………………………………………… 154
Uses and Limitations of SQLite ……………………………………………………………………. 156
16
USING PDO 157
Pros and Cons ……………………………………………………………………………………….. 158
Converting the SQLite Application ……………………………………………………………….. 158
Code Changes …………………………………………………………………………… 158
Additional Capabilities of PDO …………………………………………………………………… 161
The PDO Class ……………………………………………………………………………. 161
PDOStatement ……………………………………………………………………………. 161
Assessment ……………………………………………………………………………………………. 164
Is It the Holy Grail? ………………………………………………………………………. 164
A
SETTING UP PHP 5 165
php.ini Settings ………………………………………………………………………………………. 166
E_STRICT …………………………………………………………………………………… 167
Don’t Escape Twice ……………………………………………………………………… 168
I will try to be there ISA
after the course ISA