Understanding OOP - How to Use a PDO Connection in Other Classes

I think I have a problem in understanding how OOP works. I have already changed the code that it works, but this is not the way that I think. The following scenario (No, I do not create a user login on my own, it’s better for a local developer to understand OOP):

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 a connection (object?)

Then I have a second class, the famous User class (in fact, I do not use autoload, 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;
        }
    }
}

, . , . login. usermails . :

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

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

. , :

: undefined :: prepare()

, , .

, :

$conn database.php public, private ( , ...? , , ? ? , , ..)

, , : $this->conn->prepare $this->conn->conn->prepare user.php. , .

, user.php $this->conn = new Database(), DB, , conn->

, - . , , , , .

!:)

+8
1
  • . , PDO. , Vanilla PDO.
  • , $ db vanilla PDO .
  • ,

database.php:

<?php
$host = '127.0.0.1';
$db   = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
    \PDO::ATTR_ERRMODE            => \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

<?php
class User {
    /* Properties */
    private $conn;

    /* Get database access */
    public function __construct(\PDO $pdo) {
        $this->conn = $pdo;
    }

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

app.php

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

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

:

username_foo
username_bar
username_baz

( ) PDO PDO.

+14

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


All Articles