I was hoping if anyone could tell me if I'm doing this correctly, I know there are other questions like this but everyone codes different and I just feel that I'm not doing this right. I'm trying to make a modular OOP site so all the files will be called through the one index file which will then route the user to the correct page. All the code below works correctly (I have tested it) but I was curious about a few things.
- Is this the correct way of using OOP and classes (mainly the DB connection)?
- Why (if it is correct) when I use Netbeans PHP that it doesn't pick up the functions? See images below.
db.php (DB connection file)
$host = "localhost";
$username = "root";
$password = "";
$database = "als";
$errors = new errors();
try {
$db = new PDO("mysql:host=$host;dbname=$database", $username, $password);
} catch(PDOException $ex) {
trigger_error($ex->getMessage());
$errors->addLog($ex->getPrevious(), true);
exit;
}
User.php (class)
class user {
private $db = null;
public function __construct() {
global $db;
$this->db = $db;
}
public function deleteUser($ID) {
if(isset($ID) && !empty($ID) && is_int($ID) == TRUE) {
$deleteUser = $this->db->prepare("DELETE FROM `users` WHERE userid=?");
$deleteUser->bindParam(1, $ID, PDO::PARAM_INT);
$deleteUser->execute();
} else {
return FALSE;
}
}
}
index.php (main file)
include 'config/autoloader.php';
include 'config/db.php';
$user = new user();
$user->deleteUser(1);