Organizing Your PHP Projects

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    3 Favorites

    Organizing Your PHP Projects - Presentation Transcript

    1. Organizing Your PHP Projects Paul M. Jones php | tek, chicago May 21, 2009 1
    2. Read These • “Mythical Man-Month”, Brooks • “Art of Project Management”, Berkun • “Peopleware”, DeMarco and Lister 2
    3. Project Planning in One Lesson • Goals of organization • Look at actual projects • One lesson for organizing your PHP code base • Elements of The One Lesson • Corollary effects 3
    4. About Me • Web Architect at OmniTI <http://omniti.com> • PHP since 1999 • Solar Framework (lead) • Zend Framework (dev) • PEAR Group (2007-2008) • <http://paul-m-jones.com> 4
    5. About You • Project lead/manager? • Want to share your code with others? • Want to use code from others? • Want to reduce recurring integration issues? 5
    6. About Organizing • Security • Integration and extension • Adaptable to change • Maintenance • Consistent and predictable • Re-use rules on multiple projects 6
    7. Project Evolution Tracks One-Off Heap Standalone App Library ? Modular App Collection Framework CMS 7
    8. 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 difficult to extend 8
    9. Standalone Application • 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 9
    10. Standalone Application: Typical Main Script // Setup or bootstrapping define('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'); // Teardown include_once INCLUDE_PATH . \"inc/append.inc.php\"; 10
    11. Standalone Application: Typical Include File <?php if (! defined('APP_CONSTANT')) { die(\"Direct access not allowed.\") } // expects certain global variables ?> 11
    12. Standalone Application: File Structure index.php # main pages page1.php # page2.php # page3.php # sub/ # sub-section index.php # zim.php # gir.php # irk.php # inc/ # script includes config.inc.php # prepend.inc.php # append.inc.php # lib/ # libraries foo.class.php # Bundle1/ # Bundle2/ # 12
    13. Standalone Application: Support Structure bin/ # command-line tools cache/ # cache files cron/ # cronjob scripts css/ # stylesheets docs/ # documentation img/ # images install/ # installation scripts js/ # javascript log/ # log files sql/ # schema migrations theme/ # themes or skins tpl/ # templates -- no standard naming or structure -- index.html file in each directory 13
    14. Project Evolution Tracks One-Off Heap Standalone App Library ? Modular App Collection Framework CMS 14
    15. Modular Application • 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 15
    16. CMS • Modular application ++ • General-purpose application broker • All \"main\" scripts become sub-applications • Still in the web root, still using globals to coordinate 16
    17. Application/CMS Projects • Achievo • Eventum • Seagull* • Code • Gallery • SugarCRM Igniter* • Joomla/ • Vanilla • Coppermine Mambo • WordPress • DokuWiki • MediaWiki • Drupal • PhpMyAdmin 17
    18. Project Evolution Tracks One-Off Heap Standalone App Library ? Modular App Collection Framework CMS 18
    19. Library Collection • 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 19
    20. Library Project: Typical File Structure Foo.php # Foo Foo/ # Component.php # Foo_Component Component/ # Element1.php # Foo_Component_Element1 Element2.php # Foo_Component_Element2 ... # Bar.php # Bar Bar/ # Task.php # Bar_Task Part/ # Part1.php # Bar_Task_Part1 Part2.php # Bar_Task_Part2 ... 20
    21. Framework • Codebase • Library collection • Apps extend from it • Support structure • Bootstrap file • Public assets • Protected assets 21
    22. Library/Framework Projects • AdoDB • PAT • Seagull * • Cake • PEAR • Smarty • CgiApp • PHP Unit • Solar • Code Igniter * • Phing • SwiftMailer • EZ Components • Phly • Symfony • HtmlPurifier • Prado • WACT • Horde • Propel • WASP • Mojavi/Agavi • Savant • Zend Framework 22
    23. Project Evolution Tracks class- One-Off include/ oriented Heap function oriented Standalone App Library ? Modular App Collection Framework CMS 23
    24. About Organizing • Security • Integration and extension • Adaptable to change • Maintenance • Consistent and predictable • Re-use rules on multiple projects 24
    25. The One Lesson • Organize your code as if it will be part of a library collection. 25
    26. Elements of The One Lesson • No use of global variables • Namespace all global identifiers • Class-to-file naming convention 26
    27. 1. Stop Using Globals • No more register_globals • No more $GLOBALS • No global keyword in functions and methods 27
    28. 2. Use A Namespace • Automatic deconfliction of global identifiers • Classes (prefix, namespace) • Functions, variables, constants if you must • Use with $_SESSION, $_COOKIE, etc. keys 28
    29. Namespace Examples •class User {} •class Vendor_User {} •function get_info() •function vendor_get_info() •$_SESSION[‘prefs’] •$_SESSION[‘Vendor’][‘prefs’] 29
    30. Choosing a Namespace • Project, vendor, brand, channel • A short word or acronym, not a letter (“Z”) • A unique name, not a generic one related to a task (Date, HTML, RSS, Table, User) 30
    31. 3. Class-To-File Naming Convention • Horde, PEAR, Solar, Zend, others • Highly predictable file locations • Lends itself to autoloading • Maps well to @category, @package, and @subpackage for API documentation 31
    32. Class-to-File Naming Examples // studly-caps VendorAuthTypeKey => ... Vendor/Auth/Type/Key.php? Vendor/Auth/TypeKey.php? // underscores Vendor_Auth_TypeKey => Vendor/Auth/TypeKey.php Vendor/ # @category Vendor Auth.php # @package Vendor_Auth Auth/ # ... TypeKey.php # @subpackage TypeKey/ # ... 32
    33. Extended Effects of The One Lesson • 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 33
    34. About Organizing • Security • Integration and extension • Adaptable to change • Maintenance • Consistent and predictable • Re-use rules on multiple projects 34
    35. Summary • 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 • Not a silver bullet • A valuable jump-start and mental map 35
    36. • Questions? • Comments? • Criticism? 36
    37. Bonus: Framework Structures 37
    38. Zend Framework project |-- application | |-- bootstrap.php | |-- configs | | `-- application.ini | |-- controllers | | |-- ErrorController.php | | `-- IndexController.php | |-- models | `-- views | |-- helpers | `-- scripts | |-- error | | `-- error.phtml | `-- index | `-- index.phtml |-- library | `-- Zend |-- public | `-- index.php `-- tests |-- application | `-- bootstrap.php |-- library | `-- bootstrap.php `-- phpunit.xml 38
    39. Solar - System 39
    40. Solar - Libraries 40
    41. Thanks! • http://omniti.com • http://paul-m-jones.com • http://solarphp.com 41

    + pmjones88pmjones88, 5 months ago

    custom

    1002 views, 3 favs, 0 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1002
      • 1002 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 3
    • Downloads 41
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories