DRY betont, dass jedes Wissen in einem System eine einzige, eindeutige Darstellung haben sollte.
// WET (Write Everything Twice) Code class User { public function validateUsername($username) { if (strlen($username) < 3 || strlen($username) > 20) { throw new Exception("Username must be between 3 and 20 characters"); } if (!preg_match('/^[a-zA-Z0-9_]+$/', $username)) { throw new Exception("Username can only contain letters, numbers and underscore"); } } public function updateUsername($username) { // Same validation repeated if (strlen($username) < 3 || strlen($username) > 20) { throw new Exception("Username must be between 3 and 20 characters"); } if (!preg_match('/^[a-zA-Z0-9_]+$/', $username)) { throw new Exception("Username can only contain letters, numbers and underscore"); } // Update logic } }
class User { private function validateUsername($username) { if (strlen($username) < 3 || strlen($username) > 20) { throw new Exception("Username must be between 3 and 20 characters"); } if (!preg_match('/^[a-zA-Z0-9_]+$/', $username)) { throw new Exception("Username can only contain letters, numbers and underscore"); } } public function createUser($username) { $this->validateUsername($username); // Creation logic } public function updateUsername($username) { $this->validateUsername($username); // Update logic } }
abstract class BaseController { protected function jsonResponse($data, $status = 200) { header('Content-Type: application/json'); http_response_code($status); echo json_encode($data); } } class UserController extends BaseController { public function show($id) { $user = User::find($id); return $this->jsonResponse($user); } } class ProductController extends BaseController { public function show($id) { $product = Product::find($id); return $this->jsonResponse($product); } }
trait Loggable { private function log($message) { $timestamp = date('Y-m-d H:i:s'); file_put_contents('log.txt', "[$timestamp] $message\n", FILE_APPEND); } } class UserService { use Loggable; public function createUser($data) { $this->log("Creating new user"); // User creation logic } } class PaymentService { use Loggable; public function processPayment($amount) { $this->log("Processing payment: $amount"); // Payment processing logic } }
class Config { const DEFAULT_ITEMS_PER_PAGE = 10; const MAX_UPLOAD_SIZE = 5242880; // 5MB const ALLOWED_FILE_TYPES = ['jpg', 'png', 'pdf']; } class FileUploadService { public function validateFile($file) { if ($file['size'] > Config::MAX_UPLOAD_SIZE) { throw new Exception("File too large"); } $extension = pathinfo($file['name'], PATHINFO_EXTENSION); if (!in_array($extension, Config::ALLOWED_FILE_TYPES)) { throw new Exception("Invalid file type"); } } } class MediaController { public function index() { return $this->paginate(Config::DEFAULT_ITEMS_PER_PAGE); } }
Diese Beispiele zeigen, wie man DRY über verschiedene PHP-Funktionen wie Vererbung, Merkmale, Konstanten und die richtige Methodenextraktion anwendet. Das Ziel besteht darin, eine einzige Quelle der Wahrheit für jede bestimmte Funktionalität in Ihrer Codebasis aufrechtzuerhalten.
Das obige ist der detaillierte Inhalt vonDRY (Wiederholen Sie sich nicht). Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!