1

Anybody help me to find what is wrong with the below code? I have class student.php extends db.php.

Student.php

<?php
require_once('db.php');

class Student extends Db {
    
    public function __construct() {
        $res = $this->db_connect();
    }
}


$result = new Student();

db.php

<?php 
abstract class Db {
    protected $db_host;
    protected $db_user;
    protected $db_password;
    protected $db_name;
    public function __construct() {
        $this->db_host = 'localhost';
        $this->db_user = 'root';
        $this->db_password = '';
        $this->db_name = 'student';
    }
    protected function db_connect() {
        if(!isset($GLOBAL['db_connection'])) {
            $GLOBAL['db_connection'] = new mysqli($this->db_host, $this->db_user, $this->db_password, $this->db_name);
        }
        
        if(mysqli_connect_errno()) {
            $responseArray['status'] = '500';
            $responseArray['response'] = 'Error'. mysqli_connect_error().' Error No: '. mysqli_connect_errno();
        }
        else {
            $responseArray['status'] = '200';
            $responseArray['response'] = 'Database connection success';
        }
        
        return $responseArray;
    }
    
}

?>

it returns the result

mysqli::__construct(): (HY000/1045): Access denied for user ''@'localhost' (using password: NO)

I understand DB variables are empty that's why I get the error. but I need to make sure what's wrong with the code.

Shibin Suresh
  • 363
  • 1
  • 2
  • 9
  • 1
    Rather than `extend` the `Db` class, the `Student` class should accept an instance of the `Db` class as an argument - afterall - is the Student a database? – Professor Abronsius Jan 16 '21 at 16:29
  • Yup. Student is a database. Only one page am connecting DB nothing else. Rest are HTML code. I need to follow the oops concept that's why I followed inheritance. – Shibin Suresh Jan 16 '21 at 16:52
  • I will try the method of passing the class as an argument. – Shibin Suresh Jan 16 '21 at 16:54
  • 1
    Extending a class does not make it any more OOP than simply instantiating a class as an *object*. OOP needs to be sensibly used. "Student" is **not** a *kind of database connection*, and therefore should not extend the Db class. "Student" (or "Teacher") could plausibly extend "Person" class that has properties/methods common to persons, adding student-specific features. Then, your student class should have a `$db` property, and your constructor should set something like `$this->db = new Db()`, and the rest of your code should make queries to `$this->db->query()` etc. – Markus AO Jan 16 '21 at 17:52

1 Answers1

1

You have override __construct() method so default values are not set. Check this:

<?php
require_once('db.php');

class Student extends Db {
    
    public function __construct() {
        // call Db's constructor
        parent::__construct();

        $res = $this->db_connect();
    }
}

    
    
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
mahyard
  • 1,230
  • 1
  • 13
  • 34