Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SQL
- {
- private $_connection;
- public function __construct( $database = 'default-database', $hostname = 'default-hostname' )
- {
- $this->hostname = $hostname;
- $this->username = 'default-user';
- $this->password = 'default-password';
- $this->database = $database;
- $this->_connection = new \mysqli
- (
- $this->hostname,
- $this->username,
- $this->password,
- $this->database
- );
- }
- public function __destruct()
- {
- mysqli_close( $this->_connection );
- }
- public function execute( $query )
- {
- return $this->getConnection()->query( $query );
- }
- private function getConnection()
- {
- if ( !$this->_connection )
- die( 'Connection not initialized, aborting..' );
- return $this->_connection;
- }
- }
- // -----------
- $sql = new SQL( 'different-database' );
- OR
- $sql = new SQL( 'different-database', 'different-hostname' );
- but downside is that you can't only change hostname without providing database, or you have to write this in constructor:
- public function __construct( $database = null, $hostname = null )
- {
- $this->hostname = 'default-hostname';
- $this->username = 'default-user';
- $this->password = 'default-password';
- $this->database = 'default-database';
- if( $database !== null )
- $this->password = $database;
- if( $hostname !== null )
- $this->hostname = $hostname;
- $this->_connection = new \mysqli
- (
- $this->hostname,
- $this->username,
- $this->password,
- $this->database
- );
- }
- but then you still have to write:
- $sql = new SQL( null, 'hostname' );
- which is bad
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement