Dealing with dependencies

5 months ago

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.

Do you like this presentation?

No comments yet

Post a comment

    Login or Signup to post a comment
    Login to SlideShare
    Login to Twitter
    Edit your comment Cancel

    1 Favorite

    1 Group

    Dealing with dependencies - Presentation Transcript

    1. Dealing With Dependencies Stefan Priebsch confoo 2010 thePHP.cc Montreal
    2. Stefan Priebsch Co-Founder and Principal Consultant
    3. Premium PHP Consulting & Training. Worldwide. Sebastian Arne Stefan Bergmann Blankerts Priebsch
    4. Dependency
    5. „Dependency is the degree to which each program module relies on each one of the other modules.“
    6. Module Module Module Module
    7. Module Module Module Module
    8. global $config; Globals Superglobals $username = $_POST['username']; $password = $_POST['password']; if ($config['auth_source'] == 'ldap') { require_once APP_PATH . DS . 'inc/ldap.php'; $ldap = new LdapAuthentication(); Objects $result = $ldap->authenticate($username, $password); } else { Constants $passwords = file(APP_PATH . DS . DATA_DIR . DS . 'passwd'); $result = isset($passwords[$username]) && $passwords[$username] == $password; } if ($result) { Classes Session::set('username', $username); } return $result;
    9. Your Code G C Function Function S G F Function Function G C S F G G S
    10. Code Dependencies
    11. External Dependencies
    12. External Dependencies ● Files ● Databases ● Services ● External Programs
    13. Constants
    14. class NumberCruncher { public function load(...) { $file = BASEPATH . '/name.php'; … } }
    15. class NumberCruncher { protected $basePath = BASEPATH; public function load(...) { $file = $this->basePath . '/name.php'; ... } }
    16. class NumberCruncher { protected $basePath = BASEPATH; public function setBasePath($basePath) { $this->basePath = $basePath; } public function load(...) { $file = $this->basePath. '/name.php'; ... } }
    17. class NumberCruncher { protected $basePath; public function setBasePath($basePath) { $this->basePath = $basePath; } public function load(...) { $file = $this->basePath. '/name.php'; ... } }
    18. class NumberCruncher { protected $basePath; public function __construct($basePath) { $this->basePath = $basePath; } public function load(...) { $file = $this->basePath. '/name.php'; ... } }
    19. class NumberCruncher { public function load($file, ...) { … } }
    20. Globals
    21. class NumberCruncher { public function calculate(...) { global $log; … } }
    22. class NumberCruncher { public function calculate(...) { $log = $GLOBALS['log']; ... } }
    23. class NumberCruncher { public function calculate($log, ...) { ... } }
    24. class NumberCruncher { protected $log; public function __construct($log) { $this->log = $log; } public function calculate(...) { ... } }
    25. Superglobals
    26. class Controller { public function execute(...) { … $username = $_POST['username']; $password = $_POST['password']; ... } }
    27. class Request { protected $get; protected $post; ... public function __construct(array $get, array $post, ...) { $this->get = $get; $this->post = $post; ... } ... }
    28. class Request { ... public function get($key) public function post($key) public function cookie($name) public function server($key) ... }
    29. $request = new Request($_GET, $_POST, ...); Dependency … $controller = new Controller($request); Creation … $controller->execute(); Workflow ...
    30. bootstrap.php $request = new Request($_GET, $_POST, ...); ControllerFactory.php $controller = new Controller($request); FrontController.php $controller->execute();
    31. Dependency Injection
    32. „A technique for supplying an external dependency to a software component.“
    33. Separate Object Creation from Workflow
    34. Objects
    35. class NumberCruncher { protected $log; public function __construct($log) { $this->log = $log; } public function calculate(...) { … } }
    36. $logger = new Logger(); … $nc = new NumberCruncher($logger); $nc->calculate();
    37. class NumberCruncher { protected $log; public function __construct($log) { $this->log = $log; } public function calculate(...) { … $this->log->log(...); ... } }
    38. class NumberCruncher { protected $log; public function __construct(Logger $log) { $this->log = $log; } public function calculate(...) { … $this->log->log(...); ... } }
    39. Explicit Dependency
    40. class NumberCruncher { protected $log; public function __construct(Logger $log = null) { if ($log === null) { $log = new Logger(); } $this->log = $log; } public function calculate(...) { … $this->log->log(...); ... } }
    41. Classes
    42. class NumberCruncher { public function calculate(...) { … Logger::log(...); ... } }
    43. Implicit Dependency
    44. Just Don't
    45. Data Loading Revisited
    46. class NumberCruncher { public function load($file, ...) { … load $file ... } }
    47. class NumberCruncher { protected $dataLoader; public function __construct(DataLoader $loader) { $this->dataLoader = $loader; } public function load(...) { … $data = $this->dataLoader->load(); ... } }
    48. Interfaces
    49. class NumberCruncher { protected $dataLoader; public function __construct(DataLoaderInterface $loader) { $this->dataLoader = $loader; } public function load(...) { … $data = $this->dataLoader->load(); ... } }
    50. interface DataLoaderInterface { public function load(); }
    51. class DataLoader implements DataLoaderInterface { protected $file; public function __construct($file) { $this->file = $file; } public function load() { ... } }
    52. (Unit) Testing
    53. Mock Objects
    54. „Mock objects are simulated objects that mimic the behavior of real objects in controlled ways.“
    55. class MockDataLoader implements DataLoaderInterface { public function load() { return array( '0' => array(...), '1' => array(...), ... ); } }
    56. PHPUnit
    57. class NumberCruncherTest extends PHPUnit_Framework_TestCase { public function testNumberCrunching { $data = array( '0' => array(...), '1' => array(...), ... ); $loader = $this->getMock('DataLoaderInterface') ->expects($this->once()) ->method('load') ->will($this->returnValue($data)); $cruncher = new DataCruncher($loader); ... } }
    58. Thank You
    59. Web thePHP.cc priebsch.de Slides talks.thePHP.cc Twitter twitter.com/spriebsch E-Mail stefan@thePHP.cc

    Stefan PriebschStefan Priebsch + Follow

    708 views, 1 fav, 2 embeds more

    About this presentation

    Usage Rights

    © All Rights Reserved

    Stats

    • 1 Favorites
    • 0 Comments
    • 0 Downloads
    • 682 Views on
      SlideShare
    • 26 Views on
      Embeds
    • 708 Total Views

    Embed views

    • 15 views on http://www.berejeb.com
    • 11 views on http://www.slideshare.net

    more

    Embed views

    • 15 views on http://www.berejeb.com
    • 11 views on http://www.slideshare.net

    less

    Accessibility

    Additional Details

    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

    Groups / Events

    Follow SlideShare