How to use PDO connection in other class?
P粉548512637
P粉548512637 2023-10-23 09:37:51
0
1
710

I think I have a problem understanding how OOP works. I've changed the code so it works, but it's not the right way I think. The following scenario (no, I'm not creating the user login myself, it's really just for local developers to understand OOP better):

I have a database.php file:

class Database {

    /* Properties */
    private $conn;
    private $dsn = 'mysql:dbname=test;host=127.0.0.1';
    private $user = 'root';
    private $password = '';

    /* Creates database connection */
    public function __construct() {
        try {
            $this->conn = new PDO($this->dsn, $this->user, $this->password);
        } catch (PDOException $e) {
            print "Error!: " . $e->getMessage() . "";
            die();
        }
        return $this->conn;
    }
}

So in this class I create a database connection and return that connection (object?)

Then I have the second class, the famous User class (actually I don't use autoloading, but I know about it):

include "database.php";

class User {
    /* Properties */
    private $conn;

    /* Get database access */
    public function __construct() {
        $this->conn = new Database();
    }

    /* Login a user */
    public function login() {
        $stmt = $this->conn->prepare("SELECT username, usermail FROM user");
        if($stmt->execute()) {
            while($rows = $stmt->fetch()) {
                $fetch[] = $rows;
            }
            return $fetch;
        }
        else {
            return false;
        }
    }
}

These are my two courses. As you can see, no big deal. Now, don't get confused by the function name login - actually I'm just trying to select some usernames and usermails from the database and display them. I try to achieve this by:

$user = new User();
$list = $user->login();

foreach($list as $test) {
    echo $test["username"];
}

Here comes the problem. When I execute this code I get the following error message:

Uncaught Error: Call to undefined method Database::prepare()

And I'm not sure I really understand what causes this error.

The code runs fine when I change the following:

Change $conn in database.php to public instead of private (I think this is bad...? But when it is private, I can only do it in the database class Query is executed internally, am I right? So I should put all these queries in database class? I think this is bad because in a big project it will become very big..)

The second change I want to make is: Change $this->conn->prepare in the user.php file to $this->conn->conn->prepare. Here I really don't know why.

I mean, in the constructor of user.php I have a $this->conn = new Database() and since new Database will be The DB class returns my connection object, I really don't know why there has to be a second conn->

P粉548512637
P粉548512637

reply all(1)
P粉268284930
  • Don't create a class such as the Database class as it is useless. It makes sense to create a database wrapper if it adds some extra functionality to PDO. But given its current code, it's better to use plain PDO.
  • Create a single $db instance from a plain PDO or database class.
  • Pass it as a constructor parameter to every class that requires a database connection

Database.php:

 \PDO::ERRMODE_EXCEPTION,
    \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
    \PDO::ATTR_EMULATE_PREPARES   => false,
];
$pdo = new \PDO($dsn, $user, $pass, $opt);

user.php

conn = $pdo;
    }

    /* List all users */
    public function getUsers() {
        return $this->conn->query("SELECT username, usermail FROM user")->fetchAll();
    }
}

app.php

include 'database.php';
$user = new User($pdo);
$list = $user->getUsers();

foreach($list as $test) {
    echo $test["username"],"\n";
}

Output:

username_foo
username_bar
username_baz

Check out my (the only correct) PDO tutorial for more PDO details.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template