What are the best practices for organizing and structuring PHP code across multiple files?

When organizing and structuring PHP code across multiple files, it is important to follow a modular approach to improve readability, maintainability, and scalability. One common practice is to separate different functionalities into individual files or directories based on their purpose. It is also recommended to use namespaces and autoloading to avoid naming conflicts and simplify the inclusion of files.

// Example of organizing and structuring PHP code across multiple files

// File: config.php
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', 'password');
define('DB_NAME', 'database');

// File: database.php
<?php
class Database {
    private $connection;
    
    public function __construct() {
        $this->connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    }
    
    public function query($sql) {
        return $this->connection->query($sql);
    }
}

// File: index.php
<?php
require_once 'config.php';
require_once 'database.php';

$db = new Database();
$result = $db->query('SELECT * FROM users');
while ($row = $result->fetch_assoc()) {
    echo $row['username'] . '<br>';
}