Below is the code for database connection. & there are more methods under it.
To keep it simple i decided to make a separate class for other methods.So is there anyway i can connect to database when instantiating other class.
Db.php
class Db{
private static $_instance = null;
private $_db;
private function __construct(){
try{
$this->_db = new PDO('mysql:host = localhost;dbname=db_xoo', 'root', '');
}
catch(PDOException $e){
die($e->getMessage());
}
}
public static function get_instance(){
if(!isset(self::$_instance)){
return self::$_instance = new Db();
}
else{
return self::$_instance;
}
}
Other.php
<?php
class Other{
.
.
.
public function blah(){
database queries..
}
.
.
}
?>
Now when i instantiate class other.What should i add in other.php so that it can make automatic connection to database instead of calling Db::get_instance() everytime before creating $test = new Other()
use constract in other class
like this
class Other{
private $_db;
private function __construct(){
$this->_db = Db::get_instance();
}
public function blah(){
database queries.. $this->_db->query()
}
.
.
}
Related
I have a singleton database connection class- db.php (found by googling):
<?php
/*
* Mysql database class - only one connection alowed
*/
class db {
private $_connection;
private static $_instance; //The single instance
private $_host = "localhost";
private $_username = "user_name";
private $_password = "password";
private $_database = "database";
/*
Get an instance of the Database
#return Instance
*/
public static function getInstance() {
if(!self::$_instance) { // If no instance then make one
self::$_instance = new self();
}
return self::$_instance;
}
// Constructor
private function __construct() {
$this->_connection = new mysqli($this->_host, $this->_username,
$this->_password, $this->_database);
// Error handling
if(mysqli_connect_error()) {
trigger_error("Failed to conencto to MySQL: " . mysql_connect_error(),
E_USER_ERROR);
}
// Magic method clone is empty to prevent duplication of connection
private function __clone() { }
// Get mysqli connection
public function getConnection() {
return $this->_connection;
}
public function closeConnection(){
$this->_connection->close();
}
}
?>
To test the connectivity, If I extend that database class in ext.php like this:
<?php
class ext extends db {
private $conn;
function __construct(){
$this->connect();
if(isset($this->conn)){
echo 'Connection is established<br />';
}
$this->closeConn();
}
public function connect(){
$this->conn = parent::getInstance()->getConnection();
}
public function closeConn(){
parent::closeConnection();
}
}
?>
and in my index.php page:
<?php
spl_autoload_register(function ($class) {
include '../classes/' . $class . '.php';
});
$test = new ext();
?>
now my output is as below:
Connection is established
Fatal error: Call to a member function close() on a non-object in (line number) of db.php
my question is how can I close connection in extended class (ext.php)?
The solution would be like this:
First, create a private instance variable of parent class db in ext class, like this:
class ext extends db {
...
private $parentInstance;
...
}
Then use this instance variable $parentInstance to create and close your connection, like this:
class ext extends db {
private $conn;
private $parentInstance;
function __construct(){
$this->connect();
if(isset($this->conn)){
echo 'Connection is established<br />';
}
$this->closeConn();
}
public function connect(){
$this->parentInstance = parent::getInstance();
$this->conn = $this->parentInstance->getConnection();
}
public function closeConn(){
$this->parentInstance->closeConnection();
}
}
Sidenote: There's one small syntax error in db class as well. The closing parentheses of constructor method is missing.
I have a problem. I have 2 classes (RouteController and BaseController). In the class BaseController i have my pdo connection included, and working. And in the class RouteController i have extend the BaseController and i want that the connection also works in the RouteController class. But if i make a var_dump() it returns NULL. How do i can make that it works?
Index.php:
<?php
ini_set('display_errors', true);
error_reporting(E_ALL);
require_once 'App/Config.php';
require_once 'App/Controllers/BaseController.php';
require_once 'App/Controllers/RouteController.php';
require_once 'App/Controllers/DatabaseController.php';
$connection = new DatabaseController($config['database']['host'], $config['database']['user'], $config['database']['pass'], $config['database']['name']);
$connection = $connection->Connection();
new BaseController($connection);
new RouteController;
Here are the classes:
DatabaseController
<?php
class DatabaseController
{
private $host;
private $user;
private $pass;
private $name;
public function __construct($host, $user, $pass, $name)
{
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->name = $name;
}
public function Connection()
{
try {
$this->db = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->name, $this->user, $this->pass);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$this->db->exec("SET CHARACTER SET utf8");
return $this->db;
} catch(PDOException $e) {
die($e->getMessage());
}
}
}
BaseController
<?php
class BaseController
{
protected $connection;
public function __construct($connection)
{
$this->connection = $connection;
}
}
RouteController
<?php
class RouteController extends BaseController
{
public function __construct()
{
var_dump($this->connection); // Return NULL
}
}
I need some help please, thanks.
Sorry for my bad english.
try with this code:
<?php
class RouteController extends BaseController
{
public function __construct($connection)
{
parent::__construct($connection);
var_dump($this->connection); // Return NULL
}
}
$myObject = new RouteController($con);
UPDATE:
<?php
class BaseController
{
private $connection;
public function __construct($connection)
{
$this->connection = $connection;
}
public function getConnection() {
return $this->connection;
}
}
class RouteController extends BaseController
{
public function __construct($connection)
{
parent::__construct($connection);
/* some other construct code */
}
}
$test = 'hello';
$myObject = new RouteController($test);
var_dump($myObject->getConnection());
this work
I have class for database which is like this
class DB {
private static $_instance = null;
private $_pdo ,
$_query ,
$_results,
$_error=false,
$_count=0;
private function __construct() {
try {
$this->_pdo= new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'),Config::get('mysql/username'),Config::get('mysql/password'));
//echo 'connected';
} catch (PDOException $e) {
die($e->getMessage());
}
}
public static function getInstance(){
if (!isset(self::$_instance)) {
self::$_instance = new DB();
}
return self::$_instance;
}
public function get($table,$where){
return $this->action("select *",$table,$where);
}
}
And I have another class from where I need to use the DB class
class Validate {
private $_passed = false,
$_errors = array(),
$_db = null;
public function _construct (){
$this->_db = DB::getInstance();
}
public function check($source , $items = array()){
$check = $this->_db->get($rule_value,array($item, '=',$value));
}
My problem is when I run this program I got an error like this
Call to a member function get() on a non-object
What is the reason for this error as I already have an instance of DB class in my constructor .
Your Validate constructor isn't defined correctly, and thus isn't being called.
public function _construct (){
No constructor call means $this->_db is returning null in your check() method. It should simply have another underscore:
public function __construct() {
^
Constructors and Destructors in PHP
I have the following, however I'm unable to access the database functions outside of the initial db class?
Thanks!
database.php
class db
{
private $connection;
public function __construct()
{
$this->connection = new PDO();
}
}
admin.php
class admin
{
private $connection
public function __construct(db $connection)
{
$this->connection = $connection;
}
function myFunc()
{
// How do I access the connection here?
}
}
main.php
//include db.php
//include admin.php
$connection = new db();
$admin = new admin($connection);
// How do I access the DB here?
First of all, why are you encapsulating PDO just to class containing that one object? Cannot you use PDO directly?
One of the common practices would be to implement getter in db class, like:
class db {
...
public function getPDO(){
return $this->connection;
}
}
Another way is to re-implement every function (why would you do that?!), or use __call magic function...
Or just make $connection public ;)
Or you could extend PDO class (I'm not sure whether it'll work):
class DB extends PDO {
public function __construct ( $dsn, $username = null, $password = null, $driver_options = array()){
parent::__construct( $dsn, $username, $password, $driver_options);
... more of your stuff
}
public function myFunc(){
$this->...
}
}
ok, you really need to go and read up on object-oriented design, and access modifiers. I'll explain what you need to do here, but this is a band-aid solution, and you need to deeply understand how things are working here.
In your admin class, you defined the connection as a private attribute of the class. So in the myFunc function, you simply do $this->connection to access the connection that you created in the constructor.
In your main.php file, the object you are getting rom initializing a DB object is not the connection. It is the db object as a whole, so you can not pass the connection by itself to the admin class (it is defined as private, so nobody outside the class can view it). However, why do you need to pass it to the admin class? Managing the DB connection should be the responsibility of the DB class.
In other words, what are you trying to achieve by exposing the DB connection to the admin class?
Upate: based on the reply here is a suggested answer:
class Database {
private $connection;
public function __construct() {
$this->connection = new PDO();
}
}
class Admin {
private $db;
public function __construct() {
$this->db = new Database();
}
public function myFunc() {
$this->db->query('...');
}
}
In your main.php file:
$admin = new Admin();
$admin->myFunc();
Keep in mind, every admin object is going to create a new connection to the DB, so if you create many admin objects you might face some issues. You can get around this by declaring the DB to be a singleton.
How about this:Updated
<pre>
<?php
class DB {
private $host;
private $user;
private $pass;
private $dbase;
private $connection;
public function __construct($host,$user,$pass,$dbase)
{
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->dbase = $dbase;
$this->connection = new PDO("mysql:host=$this->host;dbname=$this->dbase", $this->user, $this->pass);
}
public function connect()
{
return $this->connection;
}
public function close()
{
unset($this->connection);
return true;
}
}
$dbh = new DB('localhost','root','','inventory');
$result = $dbh->connect()->query("SELECT * FROM products")->fetchAll(PDO::FETCH_ASSOC);
print_r($result);
?>
</pre>
Updated with files separation
database.php
class db
{
private $connection;
public function __construct()
{
$this->connection = new PDO();
}
}
admin.php
class admin
{
private $connection
public function __construct(db $connection)
{
$this->connection = $connection;
}
function myFunc()
{
return $this->connection->prepare('SQL');
}
function getConnection()
{
return $this->connection;
}
}
main.php
require_once 'database.php';
require_once 'admin.php';
$connection = new db();
$admin = new admin($connection);
$admin->myFunc()->execute();
Can anybody please guide me with a sample code to establish a database connection in php using singleton class.
class DatabaseSingleton
{
// [Singleton]
private static $instance = null;
public static function getInstance()
{
if (!self::$instance)
{
self::$instance = new self();
}
return self::$instance;
}
private function __clone(){}
// [/Singleton]
private $connection = null;
private function __construct()
{
$this->connection = mysql_connect('localhost','root','admin');
if ($this->connection)
{
mysql_select_db('my_database');
}
}
//
// crud operations go here.
//
}
$db = DatabaseSingleton::getInstance();
$db->SomeCRUDOperation();
Something like that perhaps? Very basic, but should give you a starting point.
That's how a singleton-pattern looks like:
<?php
class SingletonClass
{
static private $instance = null;
static public function getInstance()
{
if (null === self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
private function __construct(){}
private function __clone(){}
}
$singletonClass = SingletonClass::getInstance();
Now you can put random functions and parameters in there that handle your DB-stuff. I hope that answers your question.
See the manual for an example on how to implement the Singleton pattern: http://www.php.net/manual/en/language.oop5.patterns.php
Then just establish the database connection in your class constructor.
I use something like this:
class DBConn
{
static private $_db = null; // The same PDO will persist from one call to the next
private function __construct() {} // disallow calling the class via new DBConn
private function __clone() {} // disallow cloning the class
/**
* Establishes a PDO connection if one doesn't exist,
* or simply returns the already existing connection.
* #return PDO A working PDO connection
*/
static public function getConnection()
{
if (self::$_db == null) { // No PDO exists yet, so make one and send it back.
try {
self::$_db = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS);
} catch (PDOException $e) {
// Use next line for debugging only, remove or comment out before going live.
// echo 'PDO says: ' . $e->getMessage() . '<br />';
// This is all the end user should see if the connection fails.
die('<h1>Sorry. The Database connection is temporarily unavailable.</h1>');
} // end PDO connection try/catch
return self::$_db;
} else { // There is already a PDO, so just send it back.
return self::$_db;
} // end PDO exists if/else
} // end function getConnection
} // end class DBConn
/**
* And you can use it as such in a class
* */
class Post {
public function __construct(){
$this->db = DBConn::getConnection();
}
public function getPosts()
{
try {
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM posts";
foreach ($this->_dbh->query($sql) as $row) {
var_dump($row);
}
/*** close the database connection ***/
$this->_dbh = null;
} catch (PDOException $e) {
echo $e->getMessage();
}
}
}
I am using something like below
class Database
{
private $_connection;
private static $_instance; //The single instance
private $_host = "HOST";
private $_username = "USERNAME";
private $_password = "PASSWORd";
private $_database = "DATABASE";
public static function getInstance() {
if(!self::$_instance) { // If no instance then make one
self::$_instance = new self();
}
return self::$_instance;
}
private function __construct() {
$this->_connection = new mysqli($this->_host, $this->_username,
$this->_password, $this->_database);
// Error handling
if(mysqli_connect_error()) {
trigger_error("Failed to conencto to MySQL: " . mysqli_connect_error(),
E_USER_ERROR);
}
}
// Magic method clone is for prevent duplication of connection
private function __clone() { }
public function getConnection() {
return $this->_connection;
}
}
$db = Database::getInstance();
$mysqli = $db->getConnection();
$sql_query = "SELECT foo FROM etc";
$result = $mysqli->query($sql_query);