Rumah >pembangunan bahagian belakang >tutorial php >'Corak Reka Bentuk Pengaturcaraan Berorientasikan Objek PHP: Memahami Prinsip SOLID dan Aplikasinya'
Corak reka bentuk pengaturcaraan berorientasikan objek PHP sentiasa menjadi topik hangat di kalangan pembangun. Dalam artikel ini, editor PHP Strawberry akan menyelidiki prinsip SOLID untuk membantu pembaca memahami dan menggunakan prinsip reka bentuk ini. Prinsip SOLID ialah asas pengaturcaraan berorientasikan objek, termasuk prinsip tanggungjawab tunggal, prinsip terbuka dan tertutup, prinsip penggantian Liskov, prinsip pengasingan antara muka dan prinsip penyongsangan kebergantungan. Dengan mempelajari dan mengamalkan prinsip ini, pembangun boleh menulis kod PHP yang lebih fleksibel, boleh diselenggara dan berskala.
class User { private $id; private $name; private $email; public function __construct($id, $name, $email) { $this->id = $id; $this->name = $name; $this->email = $email; } public function getId() { return $this->id; } public function getName() { return $this->name; } public function getEmail() { return $this->email; } } class UserRepository { private $connection; public function __construct($connection) { $this->connection = $connection; } public function save(User $user) { $stmt = $this->connection->prepare("INSERT INTO users (name, email) VALUES (?, ?)"); $stmt->execute([$user->getName(), $user->getEmail()]); } public function find($id) { $stmt = $this->connection->prepare("SELECT * FROM users WHERE id = ?"); $stmt->execute([$id]); $result = $stmt->fetch(); if ($result) { return new User($result["id"], $result["name"], $result["email"]); } return null; } }
interface Shape { public function getArea(); } class Rectangle implements Shape { private $width; private $height; public function __construct($width, $height) { $this->width = $width; $this->height = $height; } public function getArea() { return $this->width * $this->height; } } class Circle implements Shape { private $radius; public function __construct($radius) { $this->radius = $radius; } public function getArea() { return pi() * $this->radius ** 2; } } class ShapeCalculator { public function calculateTotalArea($shapes) { $totalArea = 0; foreach ($shapes as $shape) { $totalArea += $shape->getArea(); } return $totalArea; } }
class Animal { public function eat() { echo "Animal is eating."; } } class Dog extends Animal { public function bark() { echo "Dog is barking."; } } class Cat extends Animal { public function meow() { echo "Cat is meowing."; } } function feedAnimal(Animal $animal) { $animal->eat(); } feedAnimal(new Dog()); // prints "Dog is eating." feedAnimal(new Cat()); // prints "Cat is eating."
interface Printable { public function print(); } interface Scannable { public function scan(); } interface Faxable { public function fax(); } class Printer implements Printable { public function print() { echo "Printer is printing."; } } class Scanner implements Scannable { public function scan() { echo "Scanner is scanning."; } } class FaxMachine implements Faxable { public function fax() { echo "Fax machine is faxing."; } } class MultifunctionDevice implements Printable, Scannable, Faxable { public function print() { echo "Multifunction device is printing."; } public function scan() {
Atas ialah kandungan terperinci 'Corak Reka Bentuk Pengaturcaraan Berorientasikan Objek PHP: Memahami Prinsip SOLID dan Aplikasinya'. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!