Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Server Development
Habbo Retros
Habbo Q&A
Storing a PDO instance as a static variable.
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="JayC" data-source="post: 346501" data-attributes="member: 36373"><p>Found this example that might help you out!</p><p>[CODE]</p><p></p><p>namespacePersistence\Connection\Config;</p><p></p><p>interfacePDOConfig{publicfunction getDSN();publicfunction getUsername();publicfunction getPassword();publicfunction getDriverOptions();}</p><p></p><p>classMySqlConfigimplementsPDOConfig{private $username;private $password;private $db;private $host ='localhost';private $charset ='utf8';</p><p></p><p>publicfunction __construct($username, $password, $db){</p><p>$this->username = $username;</p><p>$this->password = $password;</p><p>$this->db = $db;}</p><p></p><p>// getters and setters, etc</p><p></p><p>publicfunction getDSN(){return sprintf('mysql:host=%s;dbname=%s;charset=%s',</p><p>$this->host, $this->db, $this->charset);}</p><p></p><p>publicfunction getDriverOptions(){return[</p><p>PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,</p><p>PDO::ATTR_EMULATE_PREPARES =>false,</p><p>PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC</p><p>];}}</p><p></p><p>namespacePersistence\Connection;</p><p></p><p>usePersistence\Connection\Config\PDOConfig;</p><p></p><p>classRegistry{privatestatic $connection;privatestatic $config;</p><p></p><p>publicstaticfunction setConfig(PDOConfig $config){self::$config = $config;}</p><p></p><p>publicstatic getConnection(){if(self::$connection ===null){if(self::$config ===null){thrownewRuntimeException('No config set, cannot create connection');}</p><p>$config =self::$config;self::$connection =new \PDO($config->getDSN(), $config->getUsername(),</p><p>$config->getPassword(), $config->getDriverOptions());}returnself::$connection;}}</p><p>[/CODE]</p><p>Then, at some point (early) in your application execution cycle</p><p>[CODE]</p><p>usePersistence\Connection\Config\MySqlConfig;usePersistence\Connection\Registry;</p><p>$config =newMySqlConfig('username','password','dbname');Registry::setConfig($config);</p><p>[/CODE]</p><p></p><p>Then, you can use</p><p>Registry::getConnection();</p><p>anywhere in your code to retrieve the PDO instance.</p><p></p><p>Source:<a href="http://stackoverflow.com/questions/19848384/php-pdo-instance-as-private-static-property" target="_blank"> Here</a></p></blockquote><p></p>
[QUOTE="JayC, post: 346501, member: 36373"] Found this example that might help you out! [CODE] namespacePersistence\Connection\Config; interfacePDOConfig{publicfunction getDSN();publicfunction getUsername();publicfunction getPassword();publicfunction getDriverOptions();} classMySqlConfigimplementsPDOConfig{private $username;private $password;private $db;private $host ='localhost';private $charset ='utf8'; publicfunction __construct($username, $password, $db){ $this->username = $username; $this->password = $password; $this->db = $db;} // getters and setters, etc publicfunction getDSN(){return sprintf('mysql:host=%s;dbname=%s;charset=%s', $this->host, $this->db, $this->charset);} publicfunction getDriverOptions(){return[ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES =>false, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ];}} namespacePersistence\Connection; usePersistence\Connection\Config\PDOConfig; classRegistry{privatestatic $connection;privatestatic $config; publicstaticfunction setConfig(PDOConfig $config){self::$config = $config;} publicstatic getConnection(){if(self::$connection ===null){if(self::$config ===null){thrownewRuntimeException('No config set, cannot create connection');} $config =self::$config;self::$connection =new \PDO($config->getDSN(), $config->getUsername(), $config->getPassword(), $config->getDriverOptions());}returnself::$connection;}} [/CODE] Then, at some point (early) in your application execution cycle [CODE] usePersistence\Connection\Config\MySqlConfig;usePersistence\Connection\Registry; $config =newMySqlConfig('username','password','dbname');Registry::setConfig($config); [/CODE] Then, you can use Registry::getConnection(); anywhere in your code to retrieve the PDO instance. Source:[URL='http://stackoverflow.com/questions/19848384/php-pdo-instance-as-private-static-property'] Here[/URL] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Server Development
Habbo Retros
Habbo Q&A
Storing a PDO instance as a static variable.
Top