organizing your php projects - paul m jones · read these • “mythical man-month”, brooks •...

49
Organizing Your PHP Projects Paul M. Jones

Upload: others

Post on 11-Nov-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

Organizing Your PHP Projects

Paul M. Jones

Page 2: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

Read These• “Mythical Man-Month”, Brooks

• “Art of Project Management”, Berkun

• “Peopleware”, DeMarco and Lister

2

Page 3: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

• Examine real-world projects

• The One Lesson for organizing your project

• Elements of The One Lesson

• The One Lesson in practice

Project Planning in One Lesson

3

Page 4: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

• Web Architect

• PHP since 1999 (PHP 3)

• Solar Framework (lead)

• Savant Template System (lead)

• Zend Framework (found. contrib.)

• PEAR Group (2007-2008)

About Me

4

Page 5: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

• Project lead/manager?

• Improve team consistency?

• Want to share your code with others?

• Want to use code from others?

• Want to reduce

About You

5

Page 6: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

• Security

• Integration and extension

• Adaptable to change

• Predictable and maintainable

• Teamwork consistency

• Re-use rules on multiple projects

Goals for Organizing

6

Page 7: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

Project Research; or,

“Step 1: Study Underpinnings”

7

Page 8: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

Project Evolution Tracks

Standalone App

Library Collection Modular App

Framework CMS

One-Off Heap

?

8

Page 9: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

• No discernible architecture

• Browse directly to the scripts

• Add to it piece by piece

• Little to no separation of concerns

• All variables are global

• Unmanageable, difficult to extend

One-Off Heap

9

Page 10: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

• One-off heap ++

• Series of separate page scripts and common includes

• Installed in web root

• Each responsible for global execution environment

• Script variables still global

Standalone Application

10

Page 11: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

// Setup or bootstrappingdefine('INCLUDE_PATH', dirname(__FILE__) . '/');include_once INCLUDE_PATH . 'inc/prepend.inc.php';include_once INCLUDE_PATH . 'lib/foo.class.php';include_once INCLUDE_PATH . 'lib/View.class.php';

// Actions (if we're lucky)$foo = new Foo();$data = $foo->getData();

// Display (if we're lucky)$view = new View(INCLUDE_PATH . 'tpl/');$view->assign($data);echo $view->fetch('template.tpl');

// Teardowninclude_once INCLUDE_PATH . "inc/append.inc.php";

Standalone Application: Typical Main Script

11

Page 12: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

// expects certain globalsif (! defined('APP_CONSTANT')) {    die('Direct access not allowed.');}

Standalone Application: Typical Include File

12

Page 13: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

index.php # main pagespage1.php # page2.php # page3.php # sub/ # sub-section index.php # zim.php # gir.php # inc/ # includes config.inc.php # prepend.inc.php # lib/ # libraries foo.class.php # Bundle1/ # Bundle2/ #

Standalone Application: Typical File Structure

13

Page 14: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

bin/ # command-line toolscache/ # cache filescss/ # stylesheetsdocs/ # documentationimg/ # imagesinstall/ # installation scriptsjs/ # javascriptlog/ # log filessql/ # schema migrationstheme/ # themes or skinstpl/ # templates

-- no standard naming or structure-- index.html file in each directory

Standalone Application: Support Structure

14

Page 15: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

Project Evolution Tracks

Standalone App

Library Collection Modular App

Framework CMS

One-Off Heap

?

15

Page 16: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

• Standalone application ++

• Same file structure and script style

• One additional directory: “modules”, “plugins”, etc

• Hooks in the “main” scripts for additional behaviors

• Use global variables to coordinate between modules

Modular Application

16

Page 17: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

• Modular application ++

• General-purpose application broker

• All "main" scripts become sub-applications

• Still in the web root, still using globals to coordinate

CMS

17

Page 18: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

• Achievo

• Code Igniter*

• Coppermine

• DokuWiki

• Drupal

• Eventum

• Gallery

• Joomla/Mambo

• MediaWiki

• PhpMyAdmin

• Seagull*

• SugarCRM

Application/CMS Projects

18

Page 19: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

Project Evolution Tracks

Standalone App

Library Collection Modular App

Framework CMS

One-Off Heap

?

19

Page 20: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

• Specific, limited logic extracted from an app

• Re-used directly in unrelated applications and other libraries

• No global variables

• Class-oriented

• Can exist anywhere in the file system

Library Collection

20

Page 21: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

Foo.php # FooFoo/ # Component.php # Foo_Component Component/ # Element1.php # Foo_Component_Element1 Element2.php # Foo_Component_Element2Bar.php # BarBar/ # Task.php # Bar_Task Task/ # Part1.php # Bar_Task_Part1 Part2.php # Bar_Task_Part2

Library Project: Typical File Structure

21

Page 22: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

• Codebase

• Library collection

• Apps extend from it

• Support structure

• Bootstrap file

• Public assets

• Protected assets

Framework

22

Page 23: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

• AdoDB

• Cake

• CgiApp

• Code Igniter *

• Doctrine

• EZ Components

• HtmlPurifier

• Horde

• Lithium

• Mojavi/Agavi

• PAT

• PEAR

• PHP Unit

• Phing

• Phly

• Prado

• Savant

• Seagull *

• Smarty

• Solar

• SwiftMailer

• Symfony

• WACT

• Zend

Library/Framework Projects

23

Page 24: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

include-oriented

class-oriented

Project Evolution Tracks

Standalone App

Library Collection Modular App

Framework CMS

One-Off Heap

?

24

Page 25: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

The One Lesson; or,

“Step 2: ... ?”

25

Page 26: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

Organize your projectas if

it is a library collection.

Page 27: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

• Stop using globals

• Namespace everything

• Class-to-file naming

Elements of The One Lesson

27

Page 28: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

1. Stop Using Globals

• Stop using register_globals

• Stop using $GLOBALS

• Stop using global

28

Page 29: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

• Automatic deconfliction of identifiers

• Classes (“vendor”)

• Functions, variables, constants

• Use with $_SESSION, $_COOKIE, etc. keys

2. Namespace Everything

29

Page 30: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

• Project, client, brand, channel

• A short word or acronym, not a letter (“Z”)

• A unique name, not a generic name related to a task(Date, HTML, RSS, Table, User)

Choosing a Namespace

30

Page 31: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

PHP 5.2 “Namespaces”

31

// class User {}class Vendor_User {}$user = new Vendor_User();

// function get_info() {}function vendor_get_info()

// $_SESSION["user_prefs"]$_SESSION["Vendor_User"]["prefs"];

Page 32: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

PHP 5.3 Namespaces

32

namespace vendor;class User {}

// relative namespacenamespace vendor;$user = new User();

// absolute namespacenamespace other;$user = new \vendor\User();

Page 33: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

• Class name maps directly to file name

•Vendor_User => Vendor/User.php

• Horde, PEAR, Solar, Zend, others

• Highly predictable file locations

• Lends itself to autoloading

3. Class-To-File Naming

33

Page 34: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

// studly-caps needs preg_replace(), but:VendorAuthOpenId => ... Vendor/Auth/Open/Id.php? Vendor/Auth/OpenId.php?

// underscores just need str_replace()Vendor_Auth_OpenId => Vendor/Auth/OpenId.php

Class-to-File Naming (PHP 5.2, Horde/PEAR)

34

Page 35: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

Class-to-File (PHP 5.3, PSR-0)

35

• PEAR, Solar, Zend, Doctrine, Lithium, Symfony

\foo_bar\pkg\Main => /foo_bar/pkg/Main.php\foo_bar\pkg\Main_Sub => /foo_bar/pkg/Main/Sub.php

Page 36: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

The One Lesson In Practice; or,

“Step 3: Profit!”

36

Page 37: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

• Can be used anywhere(app, module, lib, CMS, framework)

• Structure for refactoring and additions

• Testing, profiling, and public files can parallel the same structure

• Intuitive for new developers

• No more include-path woes

Extended Effects of The One Lesson

37

Page 38: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

administrator/components/editor/files/help/images/includes/ Vendor/index.phpinstallation/language/mainbody.phpmambots/media/modules/templates/

Mambo CMS

38

Page 39: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

project/ application/ bootstrap.php configs/ controllers/ models/ views/ helpers/ scripts/ library/ Zend/ Vendor/ public index.php

Zend Framework

39

Page 40: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

app/ config/ controllers/ extensions/ index.php libraries/ models/ resources/ tests/ views/ webroot/libraries/ lithium/ vendor/

Lithium

40

Page 41: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

hello/ config/ console/ HelloKernel.phpsrc/ Application/ HelloBundle/ Bundle.php Controller/ Resources/ autoload.php vendor/ symfony/ zend/ vendor/web/

Symfony 2

41

Page 42: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

system/ config/ config.php docroot/ index.php public/ include/ Solar.php Solar/ Vendor/ script/ source/ sqlite/ tmp/

Solar

42

Page 43: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

include/ Solar/ Vendor/ App/ Page.php Page/ Layout/ Locale/ Public/ View/ Model/ Gir.php Gir/ Zim.php Zim/

Solar Apps Are Libraries Too

43

Page 44: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

Refactoring• Move from existing include-based

architectureto class-based architecture ...

• ... by functionality

• ... by script

• Then build scripts out of classes, not includes

• Then build apps out of classes, not scripts

• Leads to MVC / MVP / PAC architecture 44

Page 45: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

Summary

Page 46: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

• Organize your project as if it will be part of a library collection

• Avoid globals

• Use namespaces for deconfliction

• Use class-to-file naming convention

The One Lesson

46

Page 47: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

• Security

• Integration and extension

• Adaptable to change

• Predictable and maintainable

• Teamwork consistency

• Re-use rules on multiple projects

Goals for Organizing

47

Page 48: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

• Questions?

• Comments?

• Criticism?

48

Page 49: Organizing Your PHP Projects - Paul M Jones · Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister

49

• <http://paul-m-jones.com>

• <http://solarphp.com>

• Google for “web framework benchmarks”

Thanks!