I created a simple database connection class:
class db {
private $db_host = "host";
private $db_user = "user";
private $db_pass = "pass";
private $db_name = "db_name";
public $con;
public function __construct() {
$this->con = mysqli_connect($this->db_host, ... );
...
}
public function query($query) {
return mysqli_query($this->con,$query);
}
}
Now, I would like to use $db->query() within another class instance:
class display {
public function menu() {
this function creates a list based on a result of $db->query('SQL query')
}
plus various other functions to display different things, all using $db->query()
So what is the problem?
First, I got this working by adding 'global $db' to $disply->menu() but this is obviously a big NO NO and needs to be specified within each method.
It is probably clear now that I would like to use $db->query(SQL query) wherever and whenever I need an SQL result, not just within the global scope.
I spent the last couple of hours looking this up but to my surprise most people ask how to use something like $instance->instance->method in global.
Many thanks for your time.
Tom
A simple solution is to inject the $db object when you instance your classes
class Display {
protected $db;
public function __construct($db) {
$this->db = $db;
}
public function menu() {
// here you can use $this->db as if it was the global $db;
}
}
You need to add $db as a parameter everytime you instance your classes
$mydisplay = new Display($db);
There are other ways to achieve the same purpose. For example you can make the $db class a singleton, and add a static method to retrieve the instance from wherever you need. And there are more sophisticated ways such as using pimple to manage your dependency injections as a provider instead of doing it manually.
Related
I have what seems like a simple query but I have not found an answer elsewhere.
I have 2 classes, one called DB which essentially connects to the database and can then run queries. I instantiate it at the top of the document $db= new DB; and I can then run a series of queries on the database throughout the page.
The issue I am having is that I want to use this instance within another class I have called User.
I know I can either instantiate again but this does not make sense OR pass the instance of DB when instantiating User, for instance $user = new User($db); but considering the $db instance will be used by most classes I am going to create I am thinking there is a better solution to import it into other classes.
I have looked at the global route but I read it is bad practice + I am getting unexpected global error
class User{
global $db;
public function __construct()
{
var_dump($this-> db);
}//end constructor
}//end class
Since your DB client will be instantiated once and then used everywhere else my initial thought was to recommend passing it as a constructor parameter (dependency injection), but since you are not fan of this approach, I would recommend making your DB client a singleton class, which means it can only be instantiated once and any subsequent attempt would return the same instance everywhere.
You can see a detailed response about singleton classes in PHP at Creating the Singleton design pattern in PHP5.
As a quick example, your DB would look like similar to this:
final class DB
{
public static function getInstance()
{
static $inst = null;
if ($inst === null) {
$inst = new self();
}
return $inst;
}
private function __construct()
{
// your code here ...
}
// your code here ...
}
And then, on your User class you would just get the DB class instance:
class User {
// your code here ...
public function doSomething() {
$db = DB::getInstance();
// your code here ...
}
}
PHP does not handle scopes like Javascript does, your $db is undefined.
The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well […] Within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope.
Source: http://php.net/manual/en/language.variables.scope.php
This means that there is only a global scope and a function/method scope in PHP. So, either pass the $db instance into the method as a collaborator
class User{
public function __construct() {}
public function getInfo(Database $db) {
$db->query( /* ... */ );
}
}
$user = new User();
$db = new Database();
$user->getInfo($db);
or pass it in the constructor (dependency injection)
class User{
private $db;
public function __construct(Database $db)
{
$this->db = $db;
}
public function getInfo() {
$this->db->query( /* ... */);
}
}
$db = new Database();
$user = new User($db);
$user->getInfo();
I'm trying to learn how to properly code PHP OOP.
This is where I'm running into issues.
I created several classes that extend main Application class and I want to make things work properly.
I have main file that's index.php that looks like this:
include_once('classes/Application.php');
include_once('classes/Configuration.php');
include_once('classes/Database.php');
$app = new Application;
$config = new Configuration;
$db = new Database;
var_dump($app->db_connected);
var_dump($db->db_connected);
$db->connect($config->dbhost, $config->dbuser, $config->dbpass, $config->dbname);
var_dump($app->db_connected);
var_dump($db->db_connected);
The output is:
1. bool(false)
2. bool(false)
3. bool(false)
4. bool(true)
My main application file looks like this:
class Application {
public $db_connected = false;
}
And my Database class looks like this:
class Database extends Application {
function connect($dbhost, $dbuser, $dbpass, $dbname) {
if(!$this->db_connected) {
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$this->db_connected = true;
}
}
}
So the question is, why would line #3 of the output of index.php display false? The db_connected property has been overridden in Database class and set to TRUE, but it still returns false.
Although when accessed directly from Database class instance it shows TRUE correctly. What's the deal here?
Also when does the class EXTEND command occurs? Whenever parent class' instance is created or I have to manually create instance of the child class?
It seems you are reaching for the concept of of a static variable all instances of a class share the same static variable so using the new twice will not be an issue.
You can see the code on ideaone.
// your code goes here
class Application {
static $db_connected = false;
}
class Database extends Application {
function connect() {
if(!static::$db_connected) {
static::$db_connected = true;
}
}
}
$app = new Application;
$db = new Database;
var_dump(Application::$db_connected);
var_dump(Database::$db_connected);
$db->connect();
var_dump(Application::$db_connected);
var_dump(Database::$db_connected);
Your comment make me think you are looking for a better pattern all together. I would like to throw out some key principles namely OCP and LSP SOLID.
In this case you would avoid having Application being an instance of Database but instead use dependency injection. Here is the refactored code.
class Database {
private $db_connect = false;
public function connect () {
if(!$this->db_connect) { /* do connection */ }
}
}
class Application {
private $db;
public function setDatabse(Database $db) {
$this->db = $db;
}
public function getDatabase() {
return $this->db;
}
}
$db = new Database;
$app = new Application;
$app->setDatabase($db);
$app->getDatabase()->connect();
This line is your hint
Although when accessed directly from Database class instance it shows TRUE correctly. What's the deal here?
You have 2 instances. Above you are checking $db instance which you connected with, and then you print from $app which was never connected. They are separate entities, one is connected one is not.
Extend occurs as soon as the file is loaded, read by the php interpreter, this happens regardless of ever using the class.
Extend is called from the child and inherits everything form the class it extends. So if you call a child method in the parent, well you are doing it backwards. It goes one way, Prent -> Child.
I would use Dependance injection for the database, then you can reuse it's code.
Like this:
//parent class
class Application {
//holds a reference to the Database class
protected static $db_conn = false;
public function __construct($db){
self::$db_conn = $db;
}
}
//child class of Application
class Application2 extends Application {
public function getSomething($id){
return self::$db_conn->getbyId($id) ;
}
}
//separate utility class
class Database{
static $conn;
public function __construct( $dbhost, $dbname, $dbuser, $dbpass, $dbname) {
static::$conn = mysqli_connect($dbhost, $dbuser,$dbpass,$dbname);
}
public function getbyId( $id ){
..code to get stuff by id using $conn - previous connection ...
return $result;
}
}
$db = new Database("myhost", "myuser", "mypassw", "mybd");
$app = new Application2( $db );
$app->getSomething(1);
//create another app with the same database connection, this is the value of injecting it.
$second_app = new Application2( $db );
See you can reuse database over and over, you can replace it without changing the code in Application as long as the calls for the functions of the Database class don't change. Each thing is responsible for it's own business.
This is called separation of concerns.
Inheritance is good, when it's needed. You might have an basic application for free users of you're services and then extend that with a premium application for paid members. Sense they paid they get all the free functionality, but also the premium stuff to.
In my example above the database is something they both need, as well as other things will probably use this. Such as a login system may need a database connection, payment system might, a shopping cart might. These are all separate objects, they don't / nor should they extend off of one Master Class, that's a bad idea. Keep them separate.
STATIC
I seen mention of the :: static object operator. My example is a bit flawed when using the static property protected static $db_conn = false;
$app = new Application2( $db );
$second_app = new Application2( $db ); //assigning db 2x is not needed.
The reason for :: and the -> normal way. Is that static :: is shared across all instance of a class, and -> is just this instance of the class. I had assigned the $db class to a static variable 2 times a better way would have been like this.
//parent class
class Application {
protected static $db_conn = false;
//separate method then construct.
public function connect($db){
self::$db_conn = $db;
}
}
//we'll keep the rest of the code the same here.
$db = new Database();
$app = new Application2();
$app->connect( $db );
$second_app = new Application2();
$second_app->getSomething(1);
Now in this example $second_app never ran it's connect method. But because the first $app did and because the static for the database variable protected static $db_conn. Now all classes that have extended the Application class have a database connection. This is what static does. It's value is shared across all instance of the class. So when you see :: think all class instance and when you see -> think only this class instance. It's actually one thing I love about php, makes it so much easier to keep track of then in some other languages.
Not to confuse you but the other use of the :: is not actually needing an instance at all. Assume you have a Config class like this.
class Config{
static $db = 'hello';
static $items = array('one' => 'item 1' );
private __construct(){} // no construction allowed
static function getItem( $which ){
return self::$items[$which];
}
}
Now without ever creating an instance of the class by calling new Config() , you can simply.
echo Config::$db;
// prints hello
echo Config::getItem('one');
// prints 'item 1'
This is quite use full for config type classes. Where they are an empty shell just used to store data in and you don't need an object for them, essentially a way to keep things organized. So tying this in to the previous examples
$db = new Database(Config::$myhost, Config::$myuser, Config::$mypassw, Config::$mybd);
In your case best OOP practice is to use Mediator pattern. Concrete Mediator will be Application class:
class ApplicationBase {
private $db;
private $cfg;
public function setDb(Database $db) {
$this->db = $db; return $this;
}
public function setConfig(Config $cfg) {
$this->cfg = $cfg; return $this;
}
}
class Application extends ApplicationBase {
public function getDsn() {
return $this->cfg->getDsn();
}
public function getDbUser() {
return $this->cfg->getDbUser();
}
public function getDbPass() {
return $this->cfg->getDbPass();
}
public function getConnection() {
return $this->db->getConnection();
}
}
class AppComponent {
protected $app;
public function __construct(Application $app) {
$this->app = $app;
}
}
class Config extends AppComponent {
private $dsn;
private $dbuser;
private $dbpass;
// ... getters and setters
}
class Database extends AppComponent {
private $connection;
private function connect() {
$this->connection = new PDO(
$this->app->getDsn(),
$this->app->getUser(),
$this->app->getPass()
);
}
public function getConnection() {
if (null === $this->connection) $this->connect();
return $this->connection;
}
}
class Model extends AppComponent {
protected $table;
// Model stuff here
}
class Content extends Model {
public function getNews() {
$db = $this->app->getConnection();
return $db->query("SELECT * FROM $this->table LIMIT 5")->fetchAll();
}
}
Such architecture will be enough for simple, clean-looking applications and classes will be ready for easy unit-testing:
$app = new Application();
$cfg = new Config($app);
$db = new Database($app);
$app->setDb($db)->setConfig($cfg);
$content = new Content($app);
$news = $content->getNews();
I am trying to figure out how can I extend mysqli with my classes.
This is my db class;
class db {
protected $_server = "localhost";
protected $_user = "root";
protected $_pwd = "";
protected $_db = "test";
protected $_charset = "utf8";
function __construct(){
$this->createConnection();
}
private function createConnection(){
$this->mysqli = new mysqli($this->_server, $this->_user, $this->_pwd, $this->_db);
$this->mysqli->query("SET NAMES ".$this->_charset);
$this->mysqli->query("SET CHARACTER SET ".$this->_charset);
if($this->mysqli->connect_error) {
//die('Error ('.$this->mysqli->connect_errno.') '.$this->mysqli->connect_error());
die();
}
//$this->mysqli->close();
}
public function mysqli_fetch_object($query){
$query = $this->mysqli->query($query);
return $query->fetch_object();
}
}
and I want to extend this db class in this login class:
require_once("dbClass.php");
class login extends db {
public function __construct(){
}
public function check_login($email, $password) {
$email = $this->mysqli->real_escape_string($email);
$validateMail = filter_var($email, FILTER_VALIDATE_EMAIL);
$password = $password;
$database_check = $this->mysqli_fetch_object("SELECT * FROM accounts WHERE email = '$validateMail'");
}
}
and I get these:
Notice: Undefined property: login::$mysqli
Fatal error: Call to a member function real_escape_string() on a non-object
If you override __construct, the original constructor won't run and won't create the connection.
There is no point in extending login from db.
Instead of extending you have to use database class as a service in login class.
class login{
public function __construct($db){
$this->db = $db;
}
public function check_login($email, $password) {
$stmt = $this->db->prepare("SELECT * FROM accounts WHERE email = ?");
...
}
}
Also note that your way of running queries is wrong. You have to use prepared statements
First, you use $this->mysqli, but there's no property $mysqli in the db class. Declare it as protected, since the extended class will need access to it:
protected $mysqli;
And second, you must call parent's constructor as other people said, otherwise it won't run.
But as a side note, I suggest you don't extend the db class as login. The best approach I found so far, is to have a db access class, instantiated as a singleton for your app. This way any class that needs it will ask for a singleton instance and use it.
The advantage is obvious: all your db access will occur thru a single instance, making your app much more efficient, because everything will be done thru a single database connection. The way you're doing, as soon as you construct a login instance, a new database connection will be established.
If you keep extending the db class to other classes, like users, products, and so on, new db connections will be created, wasting resources on your server.
You should remove the constructor overload, or in your constructor method you should make a call to parent::__construct. Without it, the parent's constructor is not fired.
class login extends db {
public function __construct(){
parent::__construct();
}
I know there were some questions about this opened, but I need a more "specific" example and solution.
Here is my Example:
databse.class.php
class db{
public function connect($conStr){...}
}
func.class.php
func class{
public insert_song(){
//ineed to use the conenct method from database
//then I would INERT INTO...
}
}
Questions:
1) Should I require or extend the db class in func class?
2) If I require, will the scope of the db class functions will remain? (lets say if I have a private variable there, will it be accessible from the out side?)
No, you should not require or extend the database class.
No, private variables or methods never get available outside the class. Protected variables are available to child classes only, public variables are...public.
You could require the file in which the database class lives somewhere in your config, so you can instantiate the database class wherever and whenever you want. But, as you'll probably need only one instance of the database object, you could just instantiate it in the config and pass it around using Dependency injection.
This basically means that you pass the database object to other objects needing one. A common way of dealing with database objects is using Constructor injection, although setter injection would do as well.
What you do is something similar to this:
// config:
$db = new Database;
$db->setConnectionValues();
$fooClass = new Foo($db);
$fooClass->insertSomething();
// fooClass:
class Foo
{
private $db;
public function __construct(Database $db)
{
$this->db = $db;
}
public function insertSomething()
{
$this->db->query("INSERT");
}
}
This solves most of your dependency problems.
// changed the class name, func is not good for a class name.
class Foo {
protected $db;
public setDb($db) {
$this->db = $db;
}
public insert_song(){
//ineed to use the conenct method from database
//then I would INERT INTO...
$this->db->insert(...);
}
}
Example:
// omited the error handling.
$db = new db();
$db->connect();
$foo = new Foo();
$foo->setDb($db);
$foo->insert_songs();
As part of Abstraction, you should separate the responsibilities for your classes. Your Database class should care about your Songs (which is how you should name it) class.
If your Songs class uses the Database class, you should inject it in the constructor as follows:
<?php
class Database {
public function connect($conStr) {
/*
* Connect to database here
*/
}
}
class Songs {
private $db;
public function __construct(Database $db) {
$this->db = $db;
}
public function insert_song($song) {
/*
* Now you can use $this->db as your database object!
*/
}
}
I think the title is right but please correct me if It is mis-leading.
The problem: I have a class which wants to use the DB class, now instead of having to "global $db;" in every method I wish to use the DB object I want to be able to place the object reference in my class properties.
Still following? OK here goes:
class user
{
private $id = 0;
private $name = NULL;
private $password = NULL;
private $db;
function __construct()
{
$this->load_db();
}
private function load_db()
{
global $db;
$this->$db =& $db;
}
I get an error "Object of class db could not be converted to string" which is annoying as I can't figure out how to set the var type in PHP...
Now my question is two fold:
1) How do I fix this.
or
2) Is there a better way of doing it as this feels really "kack-handed".
Thanks in advance,
Dorjan
edit: Just to make sure I'm clear I do not want to make multiple instances of the same DB object. At least I believe this to be a good practice ^,^
If you're using $this, you only need one dollar sign. So $this->db.
private function load_db()
{
global $db;
$this->db =& $db;
}
Also if you are using php 5 you dont need to use the =& operator because objects are implicitly passed by reference. Secondly, you should inject $db into the constructor or fetch it from a Registry object instead of using global.
You should better do
class User
{
private $id = 0;
private $name = NULL;
private $password = NULL;
private $db;
function __construct($db=null)
{
$this->db = $db;
}
}
Note that you don't put a $ before variable names if you access properties via $object->. That is were the error comes from.
To use a global inside a class method is really bad practice as you somehow tie your class to that global variable. Better pass it as a parameter to the constructor or a method.
Later you can instantiate the class with
$user = new User($db)
Also note that class names by convention start with a capital letter.
This:
$this->db
Inject the db instance through the constructor
public function __construct($db)
{
$this->db = $db;
}
Yopu'll want to use something called dependency injection. This is where you "inject" an object into another object to do whatever it is the object does. In this case do database stuff
class user
{
private $id = 0;
private $name = NULL;
private $password = NULL;
private $db;
function __construct($database_object)
{
$this->load_db();
$this->$db = $database_object;
}
public function do_db_stuff()
{
$this->$db->doStuff();
}
$db = new Mysql()
$user = new User($db);
$user->do_db_stuff();