php|works 2007 organize php project

Upload: none0

Post on 31-May-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 php|works 2007 organize php project

    1/36

  • 8/14/2019 php|works 2007 organize php project

    2/36

    Project Planning in

    One Lesson Goals of organization

    Look at actual projects One lesson for organizingyour PHP code base

    Elements of The One Lesson Corollary effects

    2

  • 8/14/2019 php|works 2007 organize php project

    3/36

  • 8/14/2019 php|works 2007 organize php project

    4/36

    About Paul Savant template system PEAR involvement

    DB_Table, Text_Wiki PEAR Group representative Zend Framework

    Zend_Db, Zend_View Solar Framework for PHP5

    http://paul-m-jones.com/

    4

    http://paul-m-jones.com/http://paul-m-jones.com/http://paul-m-jones.com/
  • 8/14/2019 php|works 2007 organize php project

    5/36

    About You

    Project lead/manager?

    Want to share your codewith others?

    Want to use code fromothers?

    5

  • 8/14/2019 php|works 2007 organize php project

    6/36

    About Organizing

    Security

    Integration and extension Adaptable to change Maintenance Consistent and predictable Re-use rules on multiple projects

    6

  • 8/14/2019 php|works 2007 organize php project

    7/36

    Project Evolution Tracks

    Standalone App

    One-Off Heap

    7

    ?

  • 8/14/2019 php|works 2007 organize php project

    8/36

    One-Off Heap No discernible architecture Browse directly to the scripts Little to no separation of concerns All variables are global Add to it piece by piece Unmanageable and difcult to

    extend

    8

  • 8/14/2019 php|works 2007 organize php project

    9/36

    Standalone Application

    One-off heap ++

    Series of separate page scriptsand common includes

    Installed in web root

    Each responsible for globalexecution environment

    Script variables still "global"

    9

  • 8/14/2019 php|works 2007 organize php project

    10/36

    Standalone Application:

    File Structureindex.php # main pagestask1.php #task2.php #task3.php #

    sub/ # sub-sectionindex.php #zim.php #gir.php #irk.php #

    inc/ # script includesconfig.inc.php #prepend.inc.php #append.inc.php #

    lib/ # librariesfoo.class.php #Bundle1/ #Bundle2/ #

    10

  • 8/14/2019 php|works 2007 organize php project

    11/36

    Standalone Application:

    File Structurebin/ # command-line toolscache/ # cache filescron/ # cronjob scriptscss/ # stylesheetsdocs/ # documentationimg/ # imagesinstall/ # installation scriptsjs/ # javascriptlog/ # log files

    sql/ # sql create/update commandstheme/ # themes or skinstpl/ # templates

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

    11

  • 8/14/2019 php|works 2007 organize php project

    12/36

    Standalone Application:

    Typical Main Script// 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";

    12

  • 8/14/2019 php|works 2007 organize php project

    13/36

    Standalone Application:

    Typical Include File

    13

  • 8/14/2019 php|works 2007 organize php project

    14/36

    Project Evolution Tracks

    Standalone App

    Modular App

    CMS

    One-Off Heap

    14

  • 8/14/2019 php|works 2007 organize php project

    15/36

    Modular Application Standalone application ++ Same le 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

    15

  • 8/14/2019 php|works 2007 organize php project

    16/36

    CMS

    Modular application ++

    General-purposeapplication broker All "main" scripts become

    sub-applications

    Still in the web root, stillusing globals to coordinate

    16

  • 8/14/2019 php|works 2007 organize php project

    17/36

    Application/CMS Projects Achievo Code Igniter * Coppermine DokuWiki

    Drupal

    Eventum Gallery 2

    Joomla/Mambo MediaWiki PhpMyAdmin Seagull *

    SugarCRM

    Vanilla WordPress

    17

  • 8/14/2019 php|works 2007 organize php project

    18/36

    Project Evolution Tracks

    Standalone App

    Library Collection

    Framework

    One-Off Heap

    18

  • 8/14/2019 php|works 2007 organize php project

    19/36

    Library Collection Specic, limited logic

    extracted from an app

    Re-used directly inunrelated applications andother libraries

    No global variables Can exist anywhere in thele system (esp. w/PEAR)

    19

  • 8/14/2019 php|works 2007 organize php project

    20/36

    Library Project:

    Typical File Structure

    20

    Foo.php # FooFoo/ #

    Component.php # Foo_Component

    Component/ #Element1.php # Foo_Component_Element1Element2.php # Foo_Component_Element2... #

    Bar.php # Bar

    Bar/ #Task.php # Bar_TaskPart/ #

    Part1.php # Bar_Task_Part1Part2.php # Bar_Task_Part2...

  • 8/14/2019 php|works 2007 organize php project

    21/36

    Framework Collection of integrated libraries Standardized structure and

    naming conventions Generic bootstrap le to

    initialize environment

    Front & page controllers to loadapplication logic as a library New applications extend from it

    21

  • 8/14/2019 php|works 2007 organize php project

    22/36

    22

    Library/Framework

    Projects AdoDB Cake CgiApp EZ Components Horde Mojavi/Agavi PAT

    PEAR

    PHP Unit Phing Phly Prado Propel Savant Seagull *

    Smarty

    Solar SwiftMailer Symfony WACT WASP ZendFramework

    16

  • 8/14/2019 php|works 2007 organize php project

    23/36

    Project Evolution Tracks

    Standalone App

    Library Collection Modular App

    Framework CMS

    One-Off Heap

    23

    ?

  • 8/14/2019 php|works 2007 organize php project

    24/36

    About Organizing

    24

    Security

    Integration and extension

    Adaptable to change Maintenance Consistent and predictable Re-use rules on multiple projects

  • 8/14/2019 php|works 2007 organize php project

    25/36

    The One Lesson

    Organize your projectas if it will be part of alibrary collection.*

    (* ... if you don't choose an existing framework or CMS.)

    25

  • 8/14/2019 php|works 2007 organize php project

    26/36

    Elements of

    The One Lesson

    No use of global variables Namespace prex on all global identiers

    Class-to-le naming convention

    26

  • 8/14/2019 php|works 2007 organize php project

    27/36

    1. Stop Using Globals

    No more register_globals No more $GLOBALS No global keyword in

    functions and methods

    27

  • 8/14/2019 php|works 2007 organize php project

    28/36

    2. Use A Namespace

    Prex On Identiers No true namespaces in PHP

    Prex all global identiers forautomatic deconiction

    Classes

    Functions, variables,constants if you must

    Use with $_SESSION,$_COOKIE, etc. keys

    28

  • 8/14/2019 php|works 2007 organize php project

    29/36

    Namespace Examples

    class User {} class Vendor_User {}

    function get_info() function vendor_get_info()

    $_SESSION[prefs] $_SESSION[Vendor][prefs]

    29

  • 8/14/2019 php|works 2007 organize php project

    30/36

    Choosing a

    Namespace Prex Project, vendor, brand,

    channel

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

    A unique name, not ageneric one related to atask (Date, HTML, RSS,Table, User)

    30

  • 8/14/2019 php|works 2007 organize php project

    31/36

    3. Class-To-File Naming

    Convention Horde, PEAR, Solar, Zend, others Highly predictable le locations Lends itself to autoloading

    Maps well to @category, @package, and@subpackage for API documentation

    31

  • 8/14/2019 php|works 2007 organize php project

    32/36

    Class-to-File Naming

    Examples VendorAuthTypeKey => Vendor/Auth/Type/Key.php ?

    Vendor_Auth_TypeKey => Vendor/Auth/TypeKey.php (Not necessarily the inheritance hierarchy)

    32

    Vendor/ # @category VendorAuth.php # @package Vendor_AuthAuth/ #

    TypeKey.php # @subpackage Vendor_Auth_TypeKeyTypeKey/ #

  • 8/14/2019 php|works 2007 organize php project

    33/36

    Extended Effects of

    The One Lesson No more include-path woes Can move almost entirely outside

    web root Structure for refactoring and

    additions

    New developers can intuitstructure Testing, proling, and public les can

    parallel the same structure33

  • 8/14/2019 php|works 2007 organize php project

    34/36

    About Organizing

    34

    Security

    Integration and extension

    Adaptable to change Maintenance

    Consistent and predictable Re-use rules on multiple projects

  • 8/14/2019 php|works 2007 organize php project

    35/36

  • 8/14/2019 php|works 2007 organize php project

    36/36

    Questions? Comments?

    Criticism?