With the continuous development of technology, design patterns are becoming more and more important in software development. As the latest PHP version, PHP7.0 also integrates many design patterns. In this article, we will explore the design patterns in PHP7.0 to help PHP programmers better understand and apply these patterns.
The singleton pattern is a creational pattern that ensures that a class has only one instance and provides a global access point. In PHP7.0, you can use the __construct method and static method to implement this pattern. Here is an example:
class Singleton { private static $instance = null; private function __construct() {} public static function getInstance() { if (null === static::$instance) { static::$instance = new static(); } return static::$instance; } }
In the above code, the getInstance() method will return the only instance of the Singleton class.
Factory pattern is another creational pattern, which provides an interface for the creation of objects, but only exposes the instantiation logic of the object. In PHP7.0, you can use interfaces and abstract classes to implement this pattern. Here is an example:
interface ShapeInterface { public function draw(); } class Rectangle implements ShapeInterface { public function draw() { // 画一个矩形 } } class Square implements ShapeInterface { public function draw() { // 画一个正方形 } } abstract class ShapeFactory { public static function create($type) { switch ($type) { case 'rectangle': return new Rectangle(); case 'square': return new Square(); default: throw new Exception('Invalid shape type'); } } }
In the above code, the ShapeFactory class is an abstract factory class whose create() method creates an object based on the given category and throws an exception.
The Observer pattern is a behavioral pattern that allows one object (subject) to notify other objects (observers) that its state has changed. In PHP7.0, you can use the SplObserver and SplSubject interfaces to implement this pattern. Here is an example:
class User implements SplSubject { private $name; private $observers; public function __construct($name) { $this->name = $name; $this->observers = new SplObjectStorage(); } public function attach(SplObserver $observer) { $this->observers->attach($observer); } public function detach(SplObserver $observer) { $this->observers->detach($observer); } public function notify() { foreach ($this->observers as $observer) { $observer->update($this); } } public function getName() { return $this->name; } public function setName($name) { $this->name = $name; $this->notify(); } } class Logger implements SplObserver { public function update(SplSubject $subject) { echo 'User "' . $subject->getName() . '" has been updated.' . PHP_EOL; } }
In the above code, the User class is a subject and implements the SplSubject interface. The Logger class is an observer and implements the SplObserver interface.
The Adapter pattern is a structural pattern that allows existing classes to work with other classes, although these classes have different interfaces. In PHP7.0, you can use interfaces and abstract classes to implement this pattern. Here is an example:
interface DatabaseInterface { public function connect($host, $username, $password, $database); public function query($sql); } class MysqlDatabase implements DatabaseInterface { protected $connection; public function connect($host, $username, $password, $database) { $this->connection = mysqli_connect($host, $username, $password, $database); } public function query($sql) { return mysqli_query($this->connection, $sql); } } interface DatabaseAdapterInterface { public function query($sql); } class MysqlAdapter implements DatabaseAdapterInterface { protected $mysql; public function __construct(MysqlDatabase $mysql) { $this->mysql = $mysql; } public function query($sql) { return $this->mysql->query($sql); } }
In the above code, DatabaseAdapterInterface is the adapter interface and MysqlAdapter is the adapter class.
Summary
In this article, we discussed four design patterns in PHP7.0: singleton pattern, factory pattern, observer pattern and adapter pattern. These patterns are very useful in PHP programming and can help programmers better design and organize their code. If you haven't learned these patterns yet, now is the time to start.
The above is the detailed content of What are the design patterns in PHP7.0?. For more information, please follow other related articles on the PHP Chinese website!