Last active
August 29, 2015 14:05
-
-
Save khamer/5643e5fa9b35ec7e4dc4 to your computer and use it in GitHub Desktop.
Example using Dependency Injection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class ExampleDatabase { | |
| private $db = null; | |
| public function __construct($connection_string) | |
| { | |
| $this->db = pg_connect($connection_string); | |
| } | |
| public function query($query) | |
| { | |
| return pg_query($this->db, $query); | |
| } | |
| } | |
| /* configuration */ | |
| $container = new Pimple\Container(); | |
| $container['database'] = function() { | |
| return new ExampleDatabase('dbname=testing'); | |
| }; | |
| /* usage */ | |
| $example_db = $container['database']; | |
| $result = $example_db->query('SELECT * FROM users'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment