PHP Class Referring PDO Object - php

I am creating 2 Class and from 1 class I am referring the other class a PDO Object. But when I refer any string of that class, but not when PDO Object. Any ideas?
Here is my code
class Connection
{
private $dbcc;
public function Fn_Db_Conn()
{
$this->dbcc = new PDO( "mysql:host=localhost;dbname=db1;",
"root","pass1");
return $this->dbcc;
}
}
class Registration
{
private $Username;
private $dbc;
public function Registration($Un)
{
$this->Username = $Un;
$this->dbc = new Connection;
$this->dbc->Fn_Db_Conn();
}
public function Fn_User_Exist()
{
$Qry = "SELECT * FROM CMT_Users WHERE Username=#Username";
$Result = $this->dbc->prepare($Qry);
$Result->bindParam("#Username",$this->Username);
$Result->execute();
print $Result->rowCount();
}
}

class Connection
{
private $_dbcc;
public function getConnection()
{
return $this->_dbcc;
}
public function __construct()
{
$this->_dbcc = new PDO( "mysql:host=localhost;dbname=db1;",
"root","pass1");
}
}
class Registration
{
private $_username;
private $_dbc;
public function __construct($un)
{
$this->_username = $un;
$this->_dbc = new Connection();
}
public function Fn_User_Exist()
{
$qry = "SELECT * FROM CMT_Users WHERE Username=#Username";
$result = $this->_dbc->getConnection()->prepare($qry);
$result->bindParam("#Username",$this->_username);
$result->execute();
print $result->rowCount();
}
}
I have also modified the Connection class to create the PDO object in the constructor and added a getConnection method for accessing the PDO object.
You should use the __construct keyword for constructor, naming the constructor as the class name is the old syntax and make the code more difficult to edit.
Last point, it depends on people but I prefer to prepend protected and private properties or methods by an underscore _, this way we can identify easily if the method/property is accessible outside of the class or not. You should aboid using variable like Result because PHP is case sensitive so Result is not equal to result, so better to avoid typo to keep the variable name in lower case(appart when you want to do camelCase).

Related

Assign a public static function's return value to a private variable

There are two classes:
class Db {
public static function getConnection () {
/*Initialize parameters*/
$db = new PDO (...);
return $db;
}
}
Class Db initializes and returns a PDO object.
Then I want to do following in another class:
class User {
private $db = Db::getConnection();
....
}
Why am I getting an error here:
private $db = Db::getConnection();
Without knowing the error, it's hard to say, but I'd guess is because you can't do that there, try this.
class User {
private $db = null;
function __construct(){
$this->db = Db::getConnection();
}
public function getFriends(){
return $this->db->query('SELECT * FROM friends');
}
}

How to access object outside of class

I'm trying to access an object outside of a class. I already have a class called Database which has a series of functions in it.
$database = new Database($host,$user,$pass,$name);
class User {
function view($username) {
$user = $database->findFirst('User','username',$username);
}
}
I cannot work out how to access the $database object and keep getting an error saying Undefined variable.
You could make use of dependnecy injection - constructor injection, e.g.
class User
{
/**
* #var Database
*/
protected $db;
public function __construct(Database $database)
{
$this->db = $database;
}
public function view($username)
{
$user = $this->db->findFirst('User', 'username', $username);
}
}
$database = new Database(/* ... */);
$user = new User($database);
Regarding the question in comment: Yes, this is possible. Let's think about some basic (abstract) class that could be used as a parent for all classes that should share the same basic logic (e.g. connecting to a DB, or at least injecting a DB connection).
Such abstract class could be:
class DBObject
{
/**
* #var Database
*/
protected $db;
public function __construct(Database $db)
{
$this->db = $db;
}
}
Then our User class can extend this abstract class in this way:
class User extends DBObject
{
public function view($username)
{
$user = $this->db->findFirst('User', 'username', $username);
}
}
Keep in mind that though the abstract class can implement __construct() function, it cannot be directly instantiated. Then somewhere in the code You can do the very same as in first example:
$database = new Database(/* ... */);
$user = new User($database);
$post = new Post($database);
$database should be a class member. Try this:
$database = new Database($host,$user,$pass,$name);
class User {
protected $db;
public function __construct($database) {
$this->db = $database;
}
public function view($username) {
$user = $db->findFirst('User','username',$username);
}
}
Simply define it as global variable within the function. Yet the proper way would be to make constructors or extend it with Database class.
$database = new Database($host,$user,$pass,$name);
class User {
function view($username) {
global $database;
$user = $database->findFirst('User','username',$username);
}
}
P.S. Here is another post where you can see the same situation with this solution.

Static method calling result of mysql query

I have
class Check
{
public function __construct()
{
$this->db = new Database();
}
public function query()
{
$login = Session::get("login");
$sth = $this->db->prepare('SELECT admin FROM accounts WHERE login=:login');
$sth->execute(array(':login' => $login));
$result = $sth->fetch(PDO::FETCH_NUM);
return $result[0];
}
public static function admin()
{
echo self::query();
}
}
I have Database class in another place with PDO connection.
class Database extends PDO
{
public function __construct()
{
parent::__construct('mysql:host=localhost;dbname=name','root','pass');
$this->query('SET NAMES utf8');
}
}
So after Check::admin() code I get error:
Undefined property: View::$db
Why?
You are using a static method, that wants to use a instance variable.
Your admin method calls the query method, and the query method is using the db instance variable. As your class is not instantiated, the db variable does not exists.
My suggestion would be to make the admin method non static and use your code like this:
$mycheck = new Check();
$mycheck->admin();
or, if you are on php 5.4 and want to stick with a oneliner:
(new Check())->admin();
update
note: Do not create the db class in the constructor, but inject it:
public function __construct(Database $db)
{
$this->db = $db;
}
Sorry this is not direct answer for your question but your code has some issues so take some time and examine this and ask if it's not clear for you.
<?php
class Check {
protected $_db;
public function __construct(Database $db) {
$this->_db = $db;
}
public function query(ISession $sessionData) {
//WHY IS THE SESSION STATIC?
//$login = Session::get("login");
$sth = $this->_db->Connection()->prepare('SELECT admin FROM accounts WHERE login=:login');
$sth->execute(array(':login' => $sessionData->get("login")));
$result = $sth->fetch(PDO::FETCH_NUM);
return $result[0];
}
public function admin(ISession $sessionData) {
// REALLY BAD TO ECHO HERE
echo $this->query($sessionData);
}
}
class Database {
private $_name;
private $_password;
private $_connStr;
private $_settings;
private $_pdo;
public function __construct($connstr, $name, $password, array $settings = array()) {
$this->_name = $name;
$this->_password = $password;
$this->_connStr = $connstr;
$this->_settings = $settings;
}
public function Connection() {
if ($this->_pdo == NULL) {
$this->_pdo = new PDO($this->_connStr, $this->_name, $this->_password);
}
return $this->_pdo;
}
/* other fancy methods */
public function Close() {
$this->_pdo = NULL;
}
public function __destruct() {
$this->Close();
}
}
And i don't see why you need a Check class for all this becouse if i were you i would create somethinf like this:
$currentSession = Session::GetCurrent();
$currentSession->User()->IsInRole('admin');
Note that the session is not static and if i would write a more complete solution i would avoid Session::GetCurrent() call becouse the current session would be a field in some class' instance (HttpApplication for example).

How to use $db instead of $GLOBALS['db'] in my classes?

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'];
}
}

Nested class in php 5

I want to use a class inside other class.
This is my first class:
class mydb {
public function connect($host="localhost",$user,$pass,$data,$persistency=False) {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->data = $data;
$this->persistency = $persistency;
$this->link=#mysql_connect($this->host,$this->user,$this->pass)or die(mysql_error());
If(($this->link) AND ($this->data)) {
If(#mysql_select_db($this->data,$this->link) or die(mysql_error())) {
return True;
}
}
return False;
}
public function query($query="",$sup="") {
If($sup) {
$query=#mysql_query($query,$this->link)or die("<br /><br />".mysql_error()."<br /><br />");
} Else {
$query=#mysql_query($query,$this->link) or die("<br /><br />".mysql_error()."<br /><br />");
}
return($query);
}
}
This is my second class:
class articles {
function getpath($id) {
$sql=$db->query("select id,name from articles where id='$id'");
$row=mysql_fetch_array($sql);
return $row['name'];
}
}
I receive this error: ( Fatal error: Call to a member function query() )
You didn't create $db anywhere.
You need to create reference to the mydb class in the articles class. Assuming you create an instance of the mydb class and stored it in the variable $db, this is what you would do.
class articles {
public static $db = null;
...
self::$db->query(...);
}
articles::$db = $db;
The variable doesn't have to be static, it could be a regular class variable. You would then reference it like
$this->db->query(...);
As has been mentioned, you need an instance of the mydb class made available to articles.
Here's a simple example (an anti-pattern, perhaps):
class articles {
private $db = null; // add a class property to hold your database object
// each time we create an articles object, we'll connect to the database
public function __construct() {
$this->db = new mydb(); // add your connection params
}
public function getpath($id) {
// now we can reference the object via the class property $this->db
$sql = $this->db->query("select id,name from articles where id='$id'");
$row = mysql_fetch_array($sql);
return $row['name'];
}
// the rest of your code goes here
}
If you don't want to have to constantly create new mydb instances you can do something slightly different:
Dependency Injection
class articles {
private $db = null;
// Here we'll use an existing mydb object and pass it into our new articles
public function __construct(mydb $db) {
$this->db = $db;
}
// the rest of your code goes here
}
$db = new mydb(); // create mydb
$db->connect(); // add your params
$articles = new articles($db); // create articles using mydb from above
Singleton
class mydb {
// This is static, which is special
private static $instance = null;
public static function setInstance(mydb $db) {
self::$instance = $db;
}
public static function getInstance(mydb $db) {
return self::$instance;
}
// the rest of your code goes here
}
$db = new mydb(); // Create a mydb object
$db->connect(); // add your params
mydb::setInstance($db); // store the mydb object in the mydb class
// Later when you want to query we use the stored object:
// (and this will now work everywhere in the program--
// we don't need $db anymore)
mydb::getInstance()->query();
Analysis
Generally, you don't want to open or maintain many connections to your database. It's faster to use the same connection everywhere. Sometimes this is not appropriate, though.
The singleton is a cheap form of a Service Locator.. I.e. you create a resource (or "service") and then you have a means to locate that resource without passing a variable around with you. Singletons are a basic fix, but there are better ways to architect your applications.
You might want to read about two relevant design patterns: Service Locator and Dependency Injection. Both are described here: http://martinfowler.com/articles/injection.html

Categories