Enterprise PHP Patterns

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

    1 Favorite

    Enterprise PHP Patterns - Presentation Transcript

    1. Enterprise PHP Patterns
      • Stefan Priebsch, e-novative GmbH
    2.  
    3.  
    4.  
    5. „ Every pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over,without ever doing it the same way twice.“ (Christopher Alexander)
    6. Software Design Patterns Are
      • Blueprints for proven solutions
      • A common language for developers
      • Useful to understand frameworks
    7. They Are Not
      • The solution to all design problems
      • Ready-made implementations
      • Magic bullets
    8. Basic Patterns
    9. Basic Patterns
      • Map
      • Registry
      • Singleton
      • Subject/Observer
      • Iterator
    10. Factory
    11. Factory
      • public function fooFactory() { return new Foo(); }
    12. Factory With Testmode
      • public function fooFactory() { if (TESTMODE) { return new FooTest(); } else { return new Foo(); } }
    13. RequestHelper
    14. RequestHelper
      • Registry
      • Stores input data
      • Decouples application from superglobals
      • Centralized filtering/sanitation
      • Makes testing easier
    15. RequestHelper
      • class RequestHelper { protected $get = array(); protected $post = array(); protected $cookies = array(); public function __construct($aGet, $aPost, $aCookies) { $this->get = $aGet; $this->post = $aPost; $this->cookies = $aCookies; } public function getGetValue($aKey) public function getPostValue($aKey) public function getCookieValue($aName) public function hasCookie($aName) }
    16. Filtering RequestHelper
      • class FilteringRequestHelper { protected $get = array(); protected $post = array(); protected $cookies = array(); public function __construct($aGet, $aPost, $aCookies) { $this->get = $this->filter($aGet); $this->post = $this->filter($aPost); $this->cookies = $aCookies; } public function getGetValue($aKey) public function getPostValue($aKey) public function getCookieValue($aName) public function hasCookie($aName) }
    17. Command
    18. Command
      • Encapsulates a task
      • Selects a view
      • Allows command chaining
      • Support undoable commands
    19. Command
      • class Command { public function execute() }
    20. Validating Command
      • class Command { protected function validate() protected function doExecute() public function execute() { if ($this->validate()) { $this->doExecute(); } } }
    21. Command Selecting View
      • class Command { protected $view; public function execute() { if ($this->validate()) { $this->doExecute(); $this->view = new SuccessView(); } else { $this->view = new ErrorView(); } return $this->view->display(); } }
    22. Chained Command
      • class ChainedCommand { protected function doExecute() { $cmd = new FirstCommand(); $cmd->execute(); $cmd = new SecondCommand(); $cmd->execute(); $cmd = new ThirdCommand(); $cmd->execute(); } }
    23. Domain Model
    24. Domain Model
      • Business objects encapsulating data and behaviour
      • Similar to E/R model, but with methods and inheritance
      • Keep business logic separate from data access and presentation
    25. Domain Model
      • class Speaker { public function addPresentation($aPresentation) public function getPresentations() public function hasPresentation($aPresentation) } class Presentation { public function setSpeaker($aSpeaker) public function getSpeaker() public function addTrack($aTrack) } class Track { }
    26. Domain Model
      • class Speaker { public function addPresentation($aPresentation) public function getPresentations() public function hasPresentation($aPresentation) } class Presentation { public function addSpeaker($aSpeaker) public function getSpeakers() public function addTrack($aTrack) } class Track { }
    27. Domain Model
      • class Presentation { public function addSpeaker($aSpeaker) public function getSpeakers() public function addTrack($aTrack) } class Track { public function addPresentation($aPresentation) }
    28. Domain Model
      • class Presentation { public function addSpeaker($aSpeaker) public function getSpeakers() public function addTrack($aTrack) { $this->track = $aTrack; $aTrack->addPresentation($this); } } class Track { public function addPresentation($aPresentation) }
    29. Domain Model
      • class Speaker { public function addPresentation($aPresentation) public function getPresentations() public function hasPresentation($aPresentation) public function startSpeaking() } class Presentation { public function setSpeaker($aSpeaker) public function getSpeaker() public function addTrack($aTrack) public function startPresentation($aTime) public function endPresentation($aTime) }
    30. Front Controller
    31. Front Controller
      • Central entry point
      • Map URL to command
      • multiple Front Controllers:
        • Debug
        • Test
        • Production
      • Alternative: Page Controller
    32. Front Controller
      • class FrontController { public function __construct(RequestHelper $aRequestHelper) { $this->requestHelper = $aRequestHelper; } public function execute() { $cmd = $this->requestHelper->get('command'); $command = $this->mapCommand($cmd); print $command->execute(); } }
    33. Front Controller
      • class FrontController { public function __construct(RequestHelper $aRequestHelper) { $this->requestHelper = $aRequestHelper; $this->commandMap = new CommandMap(); } protected function mapCommand($aCommand) { return $this->commandMap->mapCommand($aCommand); } }
    34. Command Map
      • class CommandMap { protected $map = array('login' => 'LoginCommand'); public function mapCommand($aCommand) { if (!isset($this->map[$aCommand])) return new NoCommand(); return new $this->map[$aCommand]; } }
    35. Initializing the Application
      • class FrontController { protected function initApplication() public function execute() { $this->initApplication(); ... } }
    36. Applying Filters
      • class FrontController { protected $filters = array(); public function addFilter(Filter $aFilter) public function execute() { ... $result = $command->execute(); foreach($this->filters as $filter) { $result = $filter->filter($result); } print $result; } }
    37. Data Mapper
    38. Data Mapper
      • Transfers data between database and domain objects
      • Separate relational mapping from domain objects
      • Decouple domain objects from database
    39. Data Mapper
      • class SpeakerMapper { public static function load($aId) { ... $speaker = new Speaker($aId); $speaker->setValues(...); return $speaker; } public static function findByLastname($aName) protected function insert($aSpeaker) protected function update($aSpeaker) protected function delete($aId) }
    40. Literature
    41. Literature
      • Stephan Schmidt: „PHP Design Patterns“
      • Matt Zandstra: „PHP 5 Objects, Patterns, and Practise“
      • Fowler: „Patterns of Enterprise Application Architecture“
      • Joshua Kerievsky: „Refactoring to Patterns“
    42. Thank you.
    43. http://www.priebsch.de http://www.e-novative.de [email_address] Profiles at Xing, LinkedIn, Facebook
    44.  

    + Stefan PriebschStefan Priebsch, 2 years ago

    custom

    840 views, 1 favs, 0 embeds more stats

    PHP Design patterns that help you build web applica more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 840
      • 840 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 44
    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