<?php
class UserModel {
private $db;
public function __construct(PDO $db) {
$this->db = $db;
}
public function getUser($userId) {
$stmt = $this->db->prepare("SELECT * FROM users WHERE id = :userId");
$stmt->bindParam(':userId', $userId, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_ASSOC);
}
}
class UserController {
private $model;
public function __construct(UserModel $model) {
$this->model = $model;
}
public function getUser($userId) {
$user = $this->model->getUser($userId);
}
}
$dbHost = 'localhost';
$dbName = 'your_database_name';
$dbUser = 'your_username';
$dbPass = 'your_password';
$db = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=utf8", $dbUser, $dbPass);
$userModel = new UserModel($db);
$userController = new UserController($userModel);
$userId = 1;
$userController->getUser($userId);
?>