0

PHP programming is pretty new to me - so I hope that I don't ask something stupid. However, I could not find any answer to my problem :-(

I have saved my database connection in a separate .php file. Let's say "connection.inc.php"

connection.inc.php looks as follows:

$host   = "name.server.com";
$dbname = "foo";
$user   = "bar";
$pass   = "PaSW0rd";

try
{
    $DBH = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass,
    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

}
catch(PDOException $e)
{
   ...
}

In my index.php I would include the connection.inc.php with 'include_once "connection.inc.php"

Additionally, I have a class called house.class.php in which I have a method with an SQL query to my database. I call the method via 'getColor($id, $DBH)' from my index.php

Is there any way to use the method without always putting $DBH into the call?

Thanks for your help!

Tobi
  • 3
  • 2

3 Answers3

0

You should get the $DBH from the class itself rather than in the index file. Then you can have that instance of the database connection available to use class wide with other methofs in that class as well without having to re-connect for every function or having to provide every function the connection as an argument.

An example of this would look something like this:

house.class.php

<?php
  class House{

      private $DBH;

      function __construct(){
        include_once "connection.inc.php";
        $this->DBH = $DBH;
      }

      function getColor($id){
        $this->DBH // <---- use it like this instead of your $DBH old variable.
      }
  }
?>
coderodour
  • 1,072
  • 8
  • 16
0

Let's say you have a class named House with a getColor() method, and in your index.php. You can use the keyword global inside your function/method, but I am strongly against this. Example:

public function getColor(int $id)
{
    global $dbh;
    //and use it as $dbh
    $dbh->prepare(....
 }

Or you can inject it in the constructor. Example of how your class will look like

class House
{

    private $dbh;
    function __construct($dbh)
    {
         $this->dbh = $dbh;
    }

    public function getColor($id)
    {
       //$this refers to current object context
        $this->dbh->prepare.....
    }
}

And when you instantiate the House object you will pass the $DBH into the constructor. Example for your index.php:

include_once "connection.inc.php";
$house = new House($DBH);
$house->getColor($id);

Although, I suggest that you checkout Domain Driven Design, Factory and Repository pattern, and ORM like Doctrine or Eloquent to get more understanding on how to separate your application/domain logic from the database.

frz3993
  • 1,595
  • 11
  • 13
-2
class Database
{
    private $host = "localhost";
    private $dbname = "codeignitor";
    private $username = "root";
    private $password = "";
    public $conn;

    public function getConnection()
    {
        $this->conn = null;
        try {
            $this->conn = new PDO ("mysql:host=" . $this->host . ";dbname=" . $this->dbname, $this->username, $this->password);
            $this->conn->exec("set names utf8"); 
            // echo "MySqlConnected";
        } catch(PDOException $exception){ 
            echo "Connection error: " . $exception->getMessage(); 
        } 
            return $this->conn; 
    } 
}
xmaestro
  • 1,084
  • 2
  • 16
  • 44