// Bad - Mixed concerns class User { public function save() { $db = new PDO('mysql:host=localhost;dbname=app', 'user', 'pass'); $stmt = $db->prepare("INSERT INTO users (name, email) VALUES (?, ?)"); $stmt->execute([$this->name, $this->email]); } } // Good - Separated database logic class User { private string $name; private string $email; } class UserRepository { private PDO $db; public function save(User $user) { $stmt = $this->db->prepare("INSERT INTO users (name, email) VALUES (?, ?)"); $stmt->execute([$user->getName(), $user->getEmail()]); } }
Contoh yang baik memisahkan struktur data (Pengguna) daripada logik storan (UserRepository). Ini menjadikan kod lebih boleh diselenggara dan membenarkan menukar kaedah storan tanpa mengubah suai kelas Pengguna.
// Bad - Mixed validation and business logic class Order { public function process() { if (empty($this->items)) { throw new Exception('Order cannot be empty'); } if ($this->total < 0) { throw new Exception('Invalid total amount'); } // Process order... } } // Good - Separated validation class OrderValidator { public function validate(Order $order): array { $errors = []; if (empty($order->getItems())) { $errors[] = 'Order cannot be empty'; } if ($order->getTotal() < 0) { $errors[] = 'Invalid total amount'; } return $errors; } } class Order { public function process() { // Only handles order processing } }
Logik pengesahan dialihkan ke kelas pengesah khusus, membenarkan kelas Pesanan menumpukan pada logik perniagaan.
// Bad - Mixed HTML and logic class ProductPage { public function show($id) { $product = $this->getProduct($id); echo "<h1>{$product->name}</h1>"; echo "<p>Price: ${$product->price}</p>"; } } // Good - Separated presentation class ProductController { public function show($id) { $product = $this->productRepository->find($id); return $this->view->render('product/show', ['product' => $product]); } } // product/show.php template <h1><?= htmlspecialchars($product->name) ?></h1> <p>Price: $<?= htmlspecialchars($product->price) ?></p>
Contoh yang baik memisahkan logik paparan kepada templat, menjadikan kod lebih boleh diselenggara dan membenarkan pereka bentuk bekerja secara bebas.
// Bad - Mixed business logic class OrderController { public function checkout() { $order = new Order($_POST['items']); $payment = new Payment($_POST['card']); $payment->process(); $order->updateStatus('paid'); $email = new EmailService(); $email->sendConfirmation($order); } } // Good - Separated services class OrderService { private PaymentService $paymentService; private EmailService $emailService; public function processOrder(Order $order, PaymentData $paymentData): void { $this->paymentService->process($paymentData); $order->updateStatus('paid'); $this->emailService->sendConfirmation($order); } } class OrderController { public function checkout() { $this->orderService->processOrder($order, $paymentData); } }
Lapisan perkhidmatan mengendalikan logik perniagaan yang kompleks, memastikan pengawal fokus pada pengendalian permintaan.
// Bad - Hardcoded configuration class EmailSender { private $host = 'smtp.example.com'; private $port = 587; public function send($message) { // Sending logic using hardcoded values } } // Good - Separated configuration // config/mail.php return [ 'host' => 'smtp.example.com', 'port' => 587 ]; class EmailSender { private array $config; public function __construct(array $config) { $this->config = $config; } public function send($message) { // Sending logic using config values } }
Konfigurasi diasingkan daripada pelaksanaan, menjadikan kod lebih fleksibel dan boleh diselenggara. Tetapan boleh ditukar tanpa mengubah suai kod.
Atas ialah kandungan terperinci Pengasingan Kebimbangan (SoC). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!