6. Dependency Injection How can Dependency Injection be applied to the following code? <?php class Book { public function __construct() { $this ->_databaseConnection = new DatabaseConnection(); // or global $databaseConnection ; $this ->_databaseConnection = $databaseConnection ; } }
7.
8. Dependency Injection How is this? <?php class Book { public function __construct() { } public function setDatabaseConnection( $databaseConnection ) { $this ->_databaseConnection = $databaseConnection ; } } $book = new Book(); $book ->setDatabase( $databaseConnection );
9. Dependency Injection How is this? <?php class Book { public function __construct( $databaseConnection ) { … } protected function setDatabaseConnection( $databaseConnection ) { $this ->_databaseConnection = $databaseConnection ; } } $book = new Book( $databaseConnection );
10. Dependency Injection How is this? <?php class Factory { private static $_database; public static function makeBook() { $book = new Book(); $book ->setDatabase( self ::$_database ); // more injection... return $book ; } public static function setup( $database ) { …. } } $book = Factory::makeBook();
11.
12.
13.
14.
15. Factory <?php class Factory { private static $_database; public static function makeBook() { $book = new Book(); $book ->setDatabase( self ::$_database ); // more injection... return $book ; } public static function setup( $database ) { …. } } $book = Factory::makeBook();
32. Zend_Log $logger = new Zend_Log(); $logger ->log(‘log this message’, Zend_Log::INFO); $logger ->info(‘log this message’);
33. Zend_Log Log priorities Debug messages 7 Zend_Log::DEBUG Informational messages 6 Zend_Log::INFO Notice: normal but significant conditions 5 Zend_Log::NOTICE Warning: warning conditions 4 Zend_Log::WARN Error: error conditions 3 Zend_Log::ERR Critical: critical conditions 2 Zend_Log::CRIT Alert: action must be taken immediately 1 Zend_Log::ALERT Emergency: system is unusable 0 Zend_Log::EMERGE Usage Value Name
34. Zend_Log Log writers Discards all log messages. This can be useful for turning off logging during testing or for disabling logging Zend_Log_writer_Null Sends log messages to the console in the Firebug extension of Firefox Zend_Log_writer_Firebug Stores logs to database records. You need to map the level and messages to two fields within a table Zend_Log_writer_Db Stores logs to files or other streams. The ‘php://output’ stream can be used to display to the output buffer Zend_Log_writer_Stream When to use Name
35. Zend_Log $logger = new Zend_Log(); // Log Warnings, Notices and Informational messages $writerMessages = new Zend_Log_Writer_Stream ($logsPath . 'messages.log'); $messageFilter1 = new Zend_Log_Filter_Priority(Zend_Log::WARN, '>='); $messageFilter2 = new Zend_Log_Filter_Priority(Zend_Log::INFO, '<='); $writerMessages ->addFilter( $messageFilter1 ); $writerMessages ->addFilter( $messageFilter2 ); $logger ->addWriter( $writerMessages );
36.
Editor's Notes
Cons: 1. Hides dependencies - A component that uses one or more singletons is hiding crucial information about your dependencies. It doesn’t take long for calls to a singleton to creep through your code base like kudzu, slowly attaching itself to every class in the system. Exposing that dependency forces you to think about it as you use a component. It also makes it more reusable as the caller can understand its requirements and how they might be satisfied. 2. Hard to test - The hidden coupling of users on a singleton makes testing a nightmare as there is no way to mock out or inject a test instance of the singleton. Also, the state of the singleton affects the execution of a suite of tests such that they are not properly isolated from each other. 3. Hard to subclass - Since initialization occurs in a singleton in static code, it is not amenable to subclassing because subclasses inherit the initialization code without the chance to override it. 4. A singleton today is a multiple tomorrow - It’s not at all unusual to discover that you now need 2 or more of something you previously only needed one of. Hard-coding the singleton pattern into your code makes it impossible to satisfy that demand later. Think Upc_Pe_Db class..