Good Practice
in PHP
2
About Me
● Lorna Mitchell
● Twitter: @lornajane
● PHP consultant, trainer, and writer
● I live in Leeds
● Website: http://lornajane.net
3
Today's Outline
● Software Development Lifecycle
● Databases
● Source Control
● Documentation
● Open Source
4
Software Lifecycles
5
Waterfall
6
Waterfall
7
Agile
● Many different interpretations
● Some or all of:
● iterative
● customer involvement
● product is always working
● done in bursts called "sprints"
● spec can change (in a controlled way)
8
Extreme Programming
● Wildly fashionable a few years ago
● Useful ideas
● Pair programming
● Code for today (and refactor)
● TDD (Test Driven Development)
● Planning poker
● Acceptance tests
9
Databases
10
Databases
● Not a dumping ground
● Often your application bottleneck
● Worth understanding
11
Tools
● phpMyAdmin
● http://phpmyadmin.net
● Command Line
● powerful
● well-documented
12
Normalised Forms
● Guidelines for good table design
●
1st
Normal Form
● no repeating groups (e.g. comma separated lists)
●
2nd
Normal Form
● decouples entities and links them by relationship
(e.g. customer data separate from order)
●
3rd
Normal Form <- good enough!
● removes redundancy and dependent information
13
Indices
● MySQL uses indexes to quickly find things
● Foreign key contstraints
● Columns used in where clauses
14
EXPLAIN
● MySQL command
● Which indexes are used
● What kind of indexes
● Number of rows scanned
● Great for diagnosing slow queries
15
Source Control
16
Using Source Control
● Create a repository, add project
● Check out project
● Make changes
● Update to get other changes
● Commit changes to repo
17
svn log
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
r3 | lornajane | 2010­04­25 10:32:09 +0100 (Sun, 25 Apr 2010) | 1 line
adding documentation notes
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
r2 | lornajane | 2010­04­22 09:07:56 +0100 (Thu, 22 Apr 2010) | 1 line
outlining source control and design patterns sections
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
r1 | weierophinney | 2010­03­30 17:37:27 +0100 (Tue, 30 Mar 2010) | 1 lin
Added readme with outline
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
18
svn diff
Index: README.txt
===================================================================
--- README.txt (revision 3)
+++ README.txt (revision 4)
@@ -31,12 +35,20 @@
to share ideas
to raise profile
to be told you're doing it wrong!
+ (pulling up examples off our own blogs, if connection allows)
Testing
(Matthew)
QA tools and CI
including code analysis, mess detection, etc
(Lorna - QA tools; Matthew - CI)
+ Static analysis
+ code sniffer
+ demo examples, find a suitable small codebase (joindin?)
+ mess detector
+ demo examples
+ what else?
+
Deployment
19
Source Control Tools
● Subversion (svn)
● http://subversion.apache.org/
● Git (git)
● http://git-scm.com/
● Bazaar (bzr)
● http://bazaar.canonical.com/
● Mercurial (hg)
● http://mercurial.selenic.com/
20
Accessing Source Control
● IDE Plugins
● Trac (http://trac.edgewall.org/)
● TortoiseSVN
● TortoiseGit
● TortoiseBzr
● TortoiseHg
21
Centralised Source Control
user
repo
user
user
user
22
Centralised Source Control
● Single repo (repository)
● Always commit to central
● Can branch centrally
23
Distributed Source Control
repo
repo
repo
repo
repo
24
Distributed Source Control
● Many repos
● Commit to local repo
● Share changes between anywhere
● No central point
25
What If I Don't Have Source
Control?
● Get some :)
● Install subversion
● Use a hosted solution
26
Documentation
27
API Docs
● Documentation generated from source code
itself
● Follows code structure
● classes
● inheritance
● methods
● parameters
28
PHPDocumentor
29
PHPDocumentor
● PHPDocumentor
● http://www.phpdoc.org/
● Uses reflection
● Comments for additional information
● Add descriptions to the documentation
30
PHPDocumentor Comments
1 <?php
2
3 class AttendeeList {
4 private $attendees;
5 private $observers;
6
7 /**
8 * Add an attendee to the list
9 *
10 * @param integer $id Attendee identifier/array index
11 * @param string $name Full name of the attendee
12 * @access public
13 * @return boolean If attendee was successfully added
14 */
15 public function addAttendee($id, $name) {
16 $this->attendees[$id] = $name;
17 $this->notify();
18 }
19
20 }
31
Beyond API Docs
● Tutorials
● Installation instructions
● Examples
● FAQs
● Forums
● User-contributed
32
Today's Outline
● Software Development Lifecycle
● Databases
● Source Control
● Documentation
● Open Source
33
Other Topics
● System administration
● Maintenance and bugs
● Design patterns
34
Open Source
35
Open Source
● Free software
● Free as in beer
● Free as in freedom
● Licensing
● BSD
● GPL
● Avoid reinventing the wheel
36
Open Source
● You get what you give
● Participate!
37
The Local PHP Scene
● LeedsPHP http://leedsphp.org
● PHPNW http://phpnw.org.uk
● GeekUp http://geekup.org
38
Questions?
39
Good Luck
for your Future!

Goodpractice