Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is extending classes good practice?

Tags:

php

mysql

pdo

I have a PDO connection in my database class and recently I have been using this as an extension for other classes i.e. class Users extends Database this allows me to always keep a Database connection without having to have a function in my Users class.

However somebody pointed out that I shouldn't be doing this as its bad practice, why exactly is this bad practice? And how can I connect to my database class in my user class without extending?

Currently I have the call to the database inside my viewall() function I tried to put this in a __construct() function however it insisted on having parameters

I've tried the below code however I get the error message as follows:

Fatal error: Call to undefined method Database::prepare() in E:\xampp\htdocs\attendance\class.Register.php on line 13

Any ideas on how I can call on my database?

This is my code:

class.Connect.php

<?php

// Database connection PDO

class Database {

    public function __construct() {
        // Connection information
        $host   = 'localhost';
        $dbname = 'attendance';
        $user   = 'root';
        $pass   = '';

        // Attempt DB connection
        try
        {
            $this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            //echo 'Successfully connected to the database!';
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }

    }

     public function __destruct()
    {
        // Disconnect from DB
        $this->pdo = null;
        //echo 'Successfully disconnected from the database!';
    }


}

?>

class.Register.php

<?php

require 'class.Connect.php';

class Register {

    public function viewall() {
        $pdo = new Database();

        $stmt = $pdo->prepare('SELECT * FROM users');
        $stmt->execute();

    $stmt->fetch();

    }
}

$run = new Register();
$run->viewall();

?>
like image 710
GSG Avatar asked Sep 17 '13 14:09

GSG


People also ask

Why do we extend classes?

You extend a class when you want the new class to have all the same features of the original, and something more. The child class may then either add new functionalities, or override some funcionalities of the parent class.

What does it mean to extend class?

Definition and Usage The extends keyword extends a class (indicates that a class is inherited from another class). In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class.

Can you extend a class twice?

You can't extend class twice in java... Nevertheless every class in java inherits from class Object instantly, so you don't have to extend your class with Object class ;) Show activity on this post. No, Java doesn't support multiple inheritance.


1 Answers

Simple rule of thumb: if a class extends another, then that class is that parent class (only slightly altered or extended). You can pass this child class instead of the parent class. Example:

class Foo { }

class Bar extends Foo { }

function baz(Foo $foo) { }

baz(new Bar);

This works, baz() expects a Foo but also accepts a Bar, because Bar is a Foo.

Now, is your Users a Database? No. Your users are not a database. Your users use a database. If at all, you should use composition:

class User {

    protected $database;

    public function __construct(Database $database) {
        $this->database = $database;
    }

}

A class should be what its responsibilities are. The responsibility of a user management class is to manage user data. Part of that may involve talking to a database, but that doesn't mean the user management class is a database. If User extends Database, that means it can do everything the Database class can do (and more). That means you could use the User class everywhere instead of the Database class, and that doesn't make any sense. Keep responsibilities separate.

Now, it's still debatable whether that is the right structure or not, but it goes into the right direction. But you may really want to have a User class, which represents one user. You then have a UserManager or UserORM or UserStorage or whatever, which is concerned with retrieving and storing User objects in a database. This class in turn uses a Database to do just that. That keeps responsibilities clear and separated. The User class represents user data, the Database class interacts with the database, the UserORM/Manager/whatever in the middle negotiates between the two.

like image 128
deceze Avatar answered Oct 06 '22 00:10

deceze