Storing a PDO instance as a static variable.

Jaden

not so active
Aug 24, 2014
886
263
Can this be done? If so, how?

Global Class:
PHP:
<?php
/**
*   &BabboCMS - Simple content management system.
*   Using: MVC Folder Structure.
*
*   Developed and designed strictly for the use of Babbo Hotel.
*   This CMS is under copyright law and is not for release nor for sale.
*   Written by Babbo Hotel (babbo.ws), content edited by Adam (Wicked).
*   Brought to you, an exploit-free, dynamic content management system.
*
*   https://babbo.ws
*
*   @author Jaden (Moonshine), Josh
*   @since July 16, 2015
*   @version 3.0.3
*   @license https://babbo.ws
*/

require ROOT.'/vendor/autoload.php';
require MAIN.'/core.php';

__autoload('Engine');
__autoload('Route');
__autoload('Template');

Core::SetIpAddress();

Core::Load('Extensions');
Core::Load('Languages');
Core::Load('Models');

Core::$Config = require MAIN.'/config.php';

Engine::Init();

Core::$MySql = Engine::get();

Core::$MySql is literally just a predefined variable.
Engine::get() either starts a new PDO instance based on the parameters, or gets the default instance which is also predefined.
 

Jaden

not so active
Aug 24, 2014
886
263
I don't think you can use a variable.. I would have to look into this more, I think it would be better to create a method :p
Its been done with MySQL, and I've stored it using a global variable but now I'm trying to use a static variable located in the Core class.
 

Jaden

not so active
Aug 24, 2014
886
263
PHP:
<?php

/**
* Class Core
* Static functions used to help initialize the API.
*/
class Core
{
    public static $MySql;
}
That's literally it.
 

JayC

Well-Known Member
Aug 8, 2013
5,505
1,401
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;}}
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);

Then, you can use
Registry::getConnection();
anywhere in your code to retrieve the PDO instance.

Source:
 

Jaden

not so active
Aug 24, 2014
886
263
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;}}
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);

Then, you can use
Registry::getConnection();
anywhere in your code to retrieve the PDO instance.

Source:
Well I can set the variable inside of a function and it works, but I'm trying to set it in a global environment, so I only have to set it once.
 

JayC

Well-Known Member
Aug 8, 2013
5,505
1,401
Well I can set the variable inside of a function and it works, but I'm trying to set it in a global environment, so I only have to set it once.
That's what I was trying to say was you could do it in a function, and then I found this example :D Let me play around with a few things and see if I can get it.
 
Alright, after looking more into PDO and what it does as I don't use PDO, and playing around with connecting to a database I have found out a few things (Also using prior knowledge as I mainly code in mysql/C#/Java)
1. You shouldn't use static variables, ever. Static variables are a bad idea, because now everything you make is going to have to be static in order to connect, and then it will turn out to be more lengthy then what you are trying to do!
2. You can't make PDO instance into a static variable, and if you make the static method say bye bye to encapsulation :)
Sorry I couldn't help more, but I wouldn't use static if I was you.
 

Users who are viewing this thread

Top