39
Good Practice in PHP

Goodpractice

Embed Size (px)

Citation preview

Page 1: Goodpractice

Good Practice in PHP

Page 2: Goodpractice

2

About Me

● Lorna Mitchell● Twitter: @lornajane● PHP consultant, trainer, and writer● I live in Leeds● Website: http://lornajane.net

Page 3: Goodpractice

3

Today's Outline

● Software Development Lifecycle● Databases● Source Control● Documentation● Open Source

Page 4: Goodpractice

4

Software Lifecycles

Page 5: Goodpractice

5

Waterfall

Page 6: Goodpractice

6

Waterfall

Page 7: Goodpractice

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)

Page 8: Goodpractice

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

Page 9: Goodpractice

9

Databases

Page 10: Goodpractice

10

Databases

● Not a dumping ground● Often your application bottleneck● Worth understanding

Page 11: Goodpractice

11

Tools

● phpMyAdmin● http://phpmyadmin.net

● Command Line● powerful● well-documented

Page 12: Goodpractice

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

Page 13: Goodpractice

13

Indices

● MySQL uses indexes to quickly find things● Foreign key contstraints● Columns used in where clauses

Page 14: Goodpractice

14

EXPLAIN

● MySQL command● Which indexes are used● What kind of indexes● Number of rows scanned● Great for diagnosing slow queries

Page 15: Goodpractice

15

Source Control

Page 16: Goodpractice

16

Using Source Control

● Create a repository, add project● Check out project● Make changes● Update to get other changes● Commit changes to repo

Page 17: Goodpractice

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 line

Added readme with outline­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­

Page 18: Goodpractice

18

svn diffIndex: 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

Page 19: Goodpractice

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/

Page 20: Goodpractice

20

Accessing Source Control

● IDE Plugins● Trac (http://trac.edgewall.org/)● TortoiseSVN

● TortoiseGit● TortoiseBzr● TortoiseHg

Page 21: Goodpractice

21

Centralised Source Control

userrepo

user

useruser

Page 22: Goodpractice

22

Centralised Source Control

● Single repo (repository)● Always commit to central● Can branch centrally

Page 23: Goodpractice

23

Distributed Source Control

repo

repo

reporepo

repo

Page 24: Goodpractice

24

Distributed Source Control

● Many repos● Commit to local repo● Share changes between anywhere● No central point

Page 25: Goodpractice

25

What If I Don't Have Source Control?

● Get some :)● Install subversion● Use a hosted solution

Page 26: Goodpractice

26

Documentation

Page 27: Goodpractice

27

API Docs

● Documentation generated from source code itself

● Follows code structure● classes● inheritance● methods● parameters

Page 28: Goodpractice

28

PHPDocumentor

Page 29: Goodpractice

29

PHPDocumentor

● PHPDocumentor● http://www.phpdoc.org/

● Uses reflection● Comments for additional information● Add descriptions to the documentation

Page 30: Goodpractice

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 }

Page 31: Goodpractice

31

Beyond API Docs

● Tutorials● Installation instructions● Examples● FAQs● Forums● User-contributed

Page 32: Goodpractice

32

Today's Outline

● Software Development Lifecycle● Databases● Source Control● Documentation● Open Source

Page 33: Goodpractice

33

Other Topics

● System administration● Maintenance and bugs● Design patterns

Page 34: Goodpractice

34

Open Source

Page 35: Goodpractice

35

Open Source

● Free software● Free as in beer● Free as in freedom

● Licensing● BSD● GPL

● Avoid reinventing the wheel

Page 36: Goodpractice

36

Open Source

● You get what you give● Participate!

Page 37: Goodpractice

37

The Local PHP Scene

● LeedsPHP http://leedsphp.org● PHPNW http://phpnw.org.uk● GeekUp http://geekup.org

Page 38: Goodpractice

38

Questions?

Page 39: Goodpractice

39

Good Luck for your Future!