How can PHP inheritance be effectively utilized in coding practices, specifically when dealing with database connections and user classes?

Using PHP inheritance can be effectively utilized in coding practices by creating a base class for database connections and user classes. This allows for code reusability, easier maintenance, and better organization of code. By extending these base classes in specific database connection and user classes, you can inherit the common functionality while still being able to implement specific features unique to each class.

// Base class for database connection
class Database {
    protected $connection;

    public function __construct($host, $username, $password, $database) {
        $this->connection = new mysqli($host, $username, $password, $database);
    }

    public function query($sql) {
        return $this->connection->query($sql);
    }
}

// User class extending Database class
class User extends Database {
    public function getUserById($id) {
        $result = $this->query("SELECT * FROM users WHERE id = $id");
        return $result->fetch_assoc();
    }

    public function updateUserById($id, $data) {
        // Update user in database
    }
}

// Creating a new User object
$user = new User('localhost', 'username', 'password', 'database');
$userData = $user->getUserById(1);