I am new to php oop , so struggling a bit .
i have a database connection class viz :
class config{
protected $HOST = "localhost";
protected $USERNAME = "something" ;
protected $PASSWORD = "something";
protected $DATABASE = "something";
// Constructor - open DB connection
function __construct() {
try {
$this->db = new mysqli($this->HOST, $this->USERNAME, $this->PASSWORD, $this-
>DATABASE);
$this->db->autocommit(FALSE);
}
catch(Exception $e)
{
if($this->db->connect_errno > 0){
echo 'Caught exception: ', $e->getMessage(), "\n";
}
}
}
// Destructor - close DB connection
function __destruct() {
$this->db->close();
}
}
$api = new Config();
Now i have another class from which i need to perform some tasks ...but i get FATAL error .
second class :
class Myclass extends config {
function __construct(){}
public function myfunction()
{
try{
$stmt = $this->db->stmt_init(); /* Error here : Fatal error: Call to a member
function stmt_init() on a non-object */
$query = "SELECT ABC FROM table " ;
$stmt = $this->db->prepare($query); /* Error here : Fatal error: Call to a member
function prepare() on a non-object */
}
catch(){}
}
}
Please guide me with proper remedial code snippet
In your child class, you need to call the parent constructor. PHP won't automatically call parent constructor's when a child class is instantiated.
class Myclass extends config {
function __construct($h, $u, $p, $d){ parent::__construct($h, $u, $p, $d); }
Also, you don't have a $db property in the parent class, so add that
class config{
protected $db;
protected $HOST = "localhost";
protected $USERNAME = "something" ;
protected $PASSWORD = "something";
protected $DATABASE = "something";
Edit: Dependency Injection approach:
class config{
public $HOST = "localhost";
public $USERNAME = "something" ;
public $PASSWORD = "something";
public $DATABASE = "something";
}
class Myclass
{
protected $db;
function __construct($db)
{
$this->db = $db;
}
public function myfunction()
{
// do whatever with $this->db
}
}
$config = new Config();
try
{
$db = new mysqli($config->HOST, $config->USERNAME, $config->PASSWORD, $config->DATABASE);
$db->autocommit(FALSE);
}
catch(Exception $e)
{
if($db->connect_errno > 0){
echo 'Caught exception: ', $e->getMessage(), "\n";
}
}
$myclass = new Myclass($db);
Have a look into Dependency Injection. This is a favorable approach for dealing with classes that need access to a database. Instead of having all your classes extend the database, just pass the datsbase object as a parameter when creating new classes (that need the db).
Your derived class constructor is not calling the base constructor, so $this->db does not have the value it's supposed to. In PHP you must do this explicitly.
In this particular case, you should remove the derived constructor altogether since it's not doing anything. This will let PHP use the base constructor directly.
Related
I'm attempting to create a mysql query using OOP in PHP. In the code below the class "database_disconnection" does not toss any errors or notices, however the last class "database_query" gives me a notice that $database_handle is undefined. I have no idea what's causing this as I created the database_query class as a derivative class of database_connection, which include the $database_handle variable as private. Any help would be greatly appreciated!
Instantiating the objects:
include("includes/database_classes.php");
new database_connection;
new database_query('SELECT * FROM users');
and the classes:
<?php
/*----------------------------------------------------------------------------------------------------*/
class database_connection
{
private $username = 'root';
private $password = '';
private $hostname = 'localhost';
private $database = 'assistant';
private $database_handle;
function connect()
{
try
{
$database_handle = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
}
catch (PDOException $e)
{
print "Error!: " . $e ->getMessage() . "<br/>";
die();
}
}
}
/*----------------------------------------------------------------------------------------------------*/
class database_disconnection extends database_connection
{
function disconnect()
{
$database_handle = null;
}
}
/*----------------------------------------------------------------------------------------------------*/
class database_query extends database_connection
{
private $mysql_statement;
function __construct( $mysql_statement )
{
$this->mysql_statement = $mysql_statement;
foreach($database_handle->query("$mysql_statement") as $row) {
print_r($row);
}
}
}
/*----------------------------------------------------------------------------------------------------*/
?>
In your database_query constructor, you're attempting to use $database_handle before initializing it. If you notice, you only initialize in the connect function of your parent class (database_connection).
Either call connect before trying to refer to it, or initialize it in the parent's constructor, and call it (parent::__construct();).
I've started learning OOP in PHP. I managed to write code where subclass would extend the superclass which contains connection to database. Now instead of extending or using subclass, is there a way where I can make this connection class global so that any class could use it's object without having to extend it?
Please note below, I have to use $this->pdo to regard the instance of the class. Is there a way where I can instantiate an object within this class like $pdo=new PDO(); and use this object as $pdo wherever I want?
Will static class help in this scenario?
class connection
{
public $servername = "localhost";
public $username = "root";
public $password = "";
public $dbname = "carrental";
public $port="3306";
public $pdo;
function addConnection()
{
try {
$this->pdo = new PDO("mysql:host=$this->servername;port=$this->port;dbname=$this->dbname", $this->username, $this->password);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
$this->pdo->query("use $this->dbname");
}
}
Tried Singleton like below but can advise what's wrong as I get fatal error and warning.
( ! ) Fatal error: in C:\wamp\www\carRental\index.php on line 20 ( ! )
PDOException: in C:\wamp\www\carRental\index.php on line 20 Call Stack
Time Memory Function Location 1 0.0012 143752 {main}( ) ..\index.php:0 2 0.0012 144296 car->__construct( ) ..\index.php:50
3 0.0013 144272 connection->addConnection( ) ..\index.php:39
4 0.0989 150800 query ( ) ..\index.php:20
<?php
class connection
{
public $servername = "localhost";
public $username = "root";
public $password = "";
public $dbname = "carrental";
public $port="3306";
public static $pdo;
function addConnection()
{
try {
self::$pdo = new PDO("mysql:host=$this->servername;port=$this->port;dbname=$this->dbname", $this->username, $this->password);
self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
self::$pdo->query("use $this->dbname");
return self::$pdo;
}
}
class car
{
public $name;
public $maker;
public $type;
public $colour;
public $passanger;
public function __construct($param1,$param2,$param3,$param4,$param5)
{
$this->name=$param1;
$this->maker=$param2;
$this->type=$param3;
$this->colour=$param4;
$this->passanger=$param5;
connection::addConnection();
}
public function addCar()
{
$sql="INSERT INTO car(car_name,car_maker,car_type,car_colour,num_passanger)VALUES('{$this->name}','{$this->maker}', '{$this->type}','{$this->colour}','{$this->passanger}')";
$stmt = $this->$pdo->prepare($sql);
$stmt->execute();
echo "Data inserted!";
}
}
$car1=new car("Honda Accord","Honda","5 wheeler","Red",8);
$car1->addCar();
?>
As i can see this line cause problems connection::addConnection();
You are trying to call addConnection like static method.
Static function means that you don't need to create instance of class to call this function. !But! when you call this method static, You can't use non-static properties or function of this class. So all of fields should be marked as static, because otherwise you will not have any of db password, login and so on.
TL:DR
Just mark "addConnection()" as
public static function addConnection()
and you can use static property/function of class.
So I have a DB class that looks like this
class db{
private $hostname = 'localhost';
private $username = 'root';
private $password = 'root';
private $con;
public function db(){
try {
$dbh = new PDO("mysql:host=$this->hostname;dbname=myDB", $this->username, $this->password);
}catch(PDOException $e){
echo $e->getMessage();
exit();
}
$this->con = $dbh;
echo 'Connected to database<br />';
}
And my index.php
include('db.class.php');
include('todo.class.php');
include('dressTemplate.inc.php');
$db = new db;
$todo = new todo($db);
And my todo.class.php start like this
class todo{
function todo(db $db){
$this->db = $db;
}
public function render($post) {
$db &= $this->db;
But then I get this notice
Notice: Undefined variable: db in todo.class.php on line 11
Notice: Object of class db could not be converted to int in todo.class.php on line 11
How do I get db to be defined correctly in todo.class.php?
You are using &=. That is equal to $db = $db & $this->db. And first notice is there because PHP knows nothing of $db (it's undeclared yet). Second notice is because you're trying to do (null) & (object). First will be converted to int first and then 'object could not be converted' will appear, obviously (since PHP will try to treat whole expression as int)
That's it: your object variable is set correctly, but your $db variable is local and has nothing to do with it. And you're doing something strange with object via & (bitwise AND)
Tip: do not use old PHP4 way to define class constructors - unless you're using PHP4. In PHP5 there's __construct() magic method for that.
Try this:
class todo {
var $db;
__construct(&$db) {
$this->db = $db;
}
public function render($post) {
$db = &$this->db;
}
}
Your todo class should either use the __construct or a public function with the class name, e.g.
class todo {
var $db;
__construct (db $db) {
$this->db = $db;
}
// OR
public function todo(db $db){
$this->db = $db;
}
...
}
You should use a public constructor: __construct()
This works fine for me:
class db{
private $hostname = 'localhost';
private $username = 'root';
private $password = 'root';
private $con;
public function __construct(){
try {
$dbh = new PDO("mysql:host=$this->hostname;dbname=myDB", $this->username, $this->password);
}catch(PDOException $e){
echo $e->getMessage();
exit();
}
$this->con = $dbh;
echo 'Connected to database<br />';
}
}
class todo{
public function __construct(db $db){
$this->db = $db;
}
}
I need to use $GLOBALS['db'] in my classes ($db is defined in my index.php), but I don't want to use $GLOBALS['db'] when I have to call it.
I wrote this code at the beginning of my classes :
class ClassName
{
var $db;
public function __construct()
{
$this->db = $GLOBALS['db'];
}
public function test()
{
$val = $this->db->oneValue('SELECT first_name FROM users LIMIT 0, 1');
echo $val->first_name;
}
}
But I'm not enjoying this; I prefer to use directly $db in my code. Is there a solution to be able to call $GLOBALS['db'] by $db?
Simples, just inject in the constructor or a setter method: (I'm assuming $db is an object here, not an array of connection parameters etc)
class ClassName
{
protected $db;
public function __construct($db)
{
$this->setConnection($db);
//Any other constructor things you want to happen...
}
/*
* This is just here for convenience, this could be protected if you only want to set
* the db connection via the constructor
*/
public function setConnection($db)
{
$this->db = $db;
}
public function test()
{
$val = $this->db->oneValue('SELECT first_name FROM users LIMIT 0, 1');
echo $val->first_name;
}
}
As mentioned in some comments above, this is a form of dependency injection which will give you more ability to re-use code inside your project (A Good Thing TM).
I prefer using singleton pattern for databases.
this is the DB class i am using for my app.
class Database {
protected static $_dbh;
const HOST = 'localhost';
const DATABASE = 'dbname';
const USERNAME = 'username';
const PASSWORD = 'password';
private function __construct() { }
public static function getInstance() {
if(!isset($_dbh)) {
try {
#Connection String.
self::$_dbh = new PDO('mysql:host='.self::HOST.';dbname='.self::DATABASE,self::USERNAME,self::PASSWORD);
self::$_dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
#Print Errors.
echo $e->getMessage();
}
}
return self::$_dbh;
}
}
as i am using singleton pattern the connection will be re-used. you can now use the connection everywhere in your app by calling static connection method i.e
class ClassName
{
protected static $_dbh;
public function __construct() {
self::$_dbh = Database::getInstance();
}
public function test() {
$sth = self::$_dbh->query('SELECT first_name FROM users LIMIT 0, 1');
$row = $sth->fetchAll(PDO::FETCH_ASSOC);
echo $row['first_name'];
}
}
Not sure of OOP syntax to do this...
I'd like to have aclass that calls the mysqli object
class Voovid_DB {
private $host = 'localhost';
private $user = 'blahblah';
private $password = 'blahblah';
private $name = 'blahblah';
public function __contstuct(){
$dbh= new mysqli( $this->host, $this->user, $this->password, $this->name );
return $dbh;
}
//get and set methods for host, user etc... go here
}
now I'd like to access all the mysqli methods like so
$dbconnect = new Voovid_DB();
if ( $result = $dbconnect->query( "SELECT first_name, last_name FROM members WHERE member_id=9" ) ) {
while ( $row = $result->fetch_assoc() ) {
$first_name = ucfirst( $row['first_name'] );
$last_name = ucfirst( $row['last_name'] );
}
} else {
$errors = $dbconnect->error;
}
i'm new to PHP OOP and not sure how to get to the mysqli methods inside the Voovid_DB class
You have to either extend the MySQLi class, or build a proxy around it.
The easiest is probably to extend it:
class Voovid_DB extends MySQLi {
private $host = 'localhost';
private $user = 'blahblah';
private $password = 'blahblah';
private $name = 'blahblah';
public function __construct(){
// call parent (MySQLi) constructor
parent::__construct( $this->host, $this->user, $this->password, $this->name );
}
// no need for other methods, they already are there
}
Notice the extends MySQLi.
Then your second code snipet should work.
Or, build a proxy:
class Voovid_DB {
private $host = 'localhost';
private $user = 'blahblah';
private $password = 'blahblah';
private $name = 'blahblah';
private $dbh;
public function __construct(){
$this->dbh = new MySQLi($this->host, $this->user, $this->password, $this->name);
}
// this will proxy any calls to this class to MySQLi
public function __call($name, $args) {
return call_user_func_array(array($this->dbh,$name), $args);
}
}
You could define a __call method:
public function __call($method, $arguments) {
return call_user_func_array(array($this->dbh, $method), $arguments);
}
__call is invoked if an undefined or inivisible method is called.
you code is correct.
the only thing you have to do is to make sure that you define your functions in Voovid_DB as public.
private or protected methods cannot be accessed from other classes
store your mysqli object in a public field in your class then you can access it like this:
$dbconnect->mysqlField->query
Constructors aren't supposed to return anything. When you say $dbconnect = new Voovid_DB(); you would normally be trying to create a Voovid_DB object, but it looks like you're using it to try an make a mysqli object. Don't make this the constructor and call the function after you create the voovid_db object.
$obj = new voovid_DB();
$dbConnect = $obj->createMysqli();