0

I'm a beginner looking to learn more about PHP OOP, so there are things I don't know, in the ShowUsers() method, I would like to display all users in the database, but I'm not getting it, because I don't know how to call the connection property.

If I've been using encapsulation, it would be easy, but I'm using the connection property as local, and I really don't know how to call this property, how can I call it without using encapsulation?

db.php

<?php


class DbConnect {
    private $host = 'localhost';
    private $dbname = 'database';
    private $username = 'root';
    private $password = '';

    public function __construct() {
        try {
            $conn = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->username, $this->password);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch(PDOException $exception) {
            throw new Exception($exception->getMessage());
        }
    }
} 

main.php

<?php

require_once 'db.php';

class Main extends DbConnect {

    public function __construct() {
        parent::__construct();
    }

    public function ShowUsers() {
        $sql = "SELECT * FROM users";
        $result = parent::__construct()->prepare($sql); //Problem here
        $result->execute();
    }
}

$object = new Main();
$object->ShowUsers();

Note: I don't want to use encapsulation to make it work, I want to learn how to call the variable without using encapsulation, if possible.

  • 3
    What property are you trying to get? In the example above, in the parent class, you haven't assigned `$conn` to anything, so there's no way for the subclass to access it. – Aurelia Peters Feb 12 '21 at 19:41
  • 2
    Also, if you extends a class you don't have to write a function to call its parent, it will be done per inheritance. So the construct of `Main` makes no sense. – β.εηοιτ.βε Feb 12 '21 at 19:42
  • @β.εηοιτ.βε Hmm, that's true! –  Feb 12 '21 at 19:45
  • @KitPeters I created the property, the constructor method is working and connects to the database. But I don't know how to use this property in the `ShowUsers()` method and execute the query. –  Feb 12 '21 at 19:46
  • If I had created the `$conn` property using encapsulation, I could just use `$this->conn->prepare($sql)` in the `ShowUsers()` method. I think it would work like that, but I would like it to work without having to create this property through encapsulation –  Feb 12 '21 at 19:49
  • It would means you will have to make a method of your class returning `$conn`, but that would prove highly ineffective because, then, you'll have to recreate the connection each time you want to do a request. – β.εηοιτ.βε Feb 12 '21 at 19:59
  • 3
    I'm looking at the code you posted. I see that `$conn` is created as a local variable in `__construct()`, but never assigned to an object property. Unless it's assigned to an object property (e.g. `protected $conn`) it won't be accessible outside of the `try`/`catch` block you've declared it in. – Aurelia Peters Feb 12 '21 at 19:59

1 Answers1

1

Based on the code above and the comments, I recommend that you declare $conn as protected in your DbConnect class:

<?php
// db.php  

class DbConnect {
    private $host = 'localhost';
    private $dbname = 'database';
    private $username = 'root';
    private $password = '';
    protected $conn;
    public function __construct() {
        try {
            $this->conn = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->username, $this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch(PDOException $exception) {
            throw new Exception($exception->getMessage());
        }
    }
} 

Then in main.php, you can do:

<?php
// main.php

require_once 'db.php';

class Main extends DbConnect {

    public function __construct() {
        parent::__construct();
    }

    public function ShowUsers() {
        $sql = "SELECT * FROM users";
        $result = $this->conn->prepare($sql);
        $result->execute();
    }
}

$object = new Main();
$object->ShowUsers();
?>
Aurelia Peters
  • 2,169
  • 1
  • 20
  • 34