How to use $ db from another .php to another .php class using OOP?

I am new to OOP PHP and I have been working with the procedural API since I started web development, so it is difficult for me to migrate to OOP.

so let's say I have four files .phpand structures below.

connection.db.php

<?php
    define("DB_HOST", "127.0.0.1");
    define("DB_USER", "root");
    define("DB_PASS", "");
    define("DB_NAME", "sample_db");

    $db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

    echo (!$db->connect_error) ? NULL : die("<pre>Unable to connect to the MySQL Server -> $db->connect_error</pre>");
?>

sampleclass.class.php

<?php
    public $db;

    class MySQLqueries {
        public function samplefunction($queryString) {
            $sqlQry = mysqli->query($queryString);

            return ($sqlQry) ? "<pre>Query Executed Successfully</pre>" : die("<pre>An error occured -> $db->error</pre>");
        }
    }
?>

includes.inc.php

<?php
    error_reporting(0);
    date_default_timezone_set("Asia/Manila");

    require 'connection.db.php';
    require 'sampleclass.class.php';
?>

index.php

<?php
    require 'includes.inc.php';

    $todo = new MySQLqueries;
    echo $todo->samplefunction("SELECT `sample_column` FROM `sample_table` WHERE `sample_column` = 'sample_value';");
?>

As you may or may not have noticed, my problem is how to use $dbfrom connection.db.phpin samplefunctionfromsampleclass.class.php

You can ask me why not create a method __construct()in sampleclass.class.php and just move "$ db = new mysqli (DB_HOST, DB_USER, DB_PASS, DB_NAME)"; there?. well, if I do this, all other classes should have their own constructorand $db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);declared there correctly?

$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); connection.db.php , .

, public $db; sampleclass.class.php, , . - , .

PS: Google Ph.D. , , , OOP PHP-MySQLi Programming, .

+3
3

DB , , .

- /.. .

, Injection Dependency, , , .

:

. , , , PSR-4 Composer.

, , PHPTheRightWay, , Injection Dependency.

- . , , , :

Class DB {

    function __construct($host, $user, $pass, $db) { 
        return $this->connect($host, $user, $pass, $db); 
    }

    function connect($host, $user, $pass, $db) {
        //..connect and all.
    }

    //...the rest of your functions/class...
}

. mysqli , .

. ;

Class Foo {

    $private $db;

    // your construct method here will ONLY except a `DB` class instance/object as $db. 
    // Try it with anything else and learn from the errors to understand what I mean.
    function __construct(DB $db){
        $this->db = $db;
    }

}

$db = new DB($host, $user, $pass, $db);
// you can error check it here

$foo = new Foo($db);// inject the $db object.

, global, , .

include('connection.db.php');

class MySQLqueries {
        public function samplefunction($queryString) {
            global $db;
            $sqlQry = mysqli->query($queryString);

            return ($sqlQry) ? "<pre>Query Executed Successfully</pre>" : die("<pre>An error occured -> $db->error</pre>");
        }
}

, global $db , $this->db.

+7

, , , , (- ).

, $db sampleclass.class.php, :

class MysqlConn
{
    public static $db;
}

mysqli :

MysqlConn::$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

, $db, :

<?php
class MysqlConn
{
    protected
        static $DB_HOST = "127.0.0.1",
        static $DB_USER = "root",
        static $DB_PASS = "",
        static $DB_NAME = "sample_db";

    protected static $db;

    public static function getDB()
    {
        if (!isset(self::$db) {
            self::$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

            if ($db->connect_error) {
                die("<pre>Unable to connect to the MySQL Server -> $db->connect_error</pre>");
            }
        }
        return self::$db;
    }
}

MysqlConn::getDB();

index.php db , :

$db = MysqlConn::getDB();
+5

, $db connection.db.php samplefunction sampleclass.class.php

connection.db.php, global php.

-

   require_once 'connection.db.php';

        class MySQLqueries {
                public function samplefunction($queryString) {
                    global $db;
                    //You can now use $db variable as you want
                    print_r($db);
                    $sqlQry = mysqli->query($queryString);

                    return ($sqlQry) ? "<pre>Query Executed Successfully</pre>" : die("<pre>An error occured -> $db->error</pre>");
                }
        }
0

Source: https://habr.com/ru/post/1624038/


All Articles