Use the Memento Design Pattern | PHP Code Examples in 2023

Using the Memento Pattern in PHP The memento pattern is a behavioral design pattern which lets you save and restore an object to its previous state without compromising its privacy. Article Highlights The memento pattern delegates creating a snapshot to the original object while keeping the state in a memento object. Benefit -   Doesn’t violate the data encapsulation of the original objects. Con -        Creating memento objects too often can consume much memory. Memento Design Pattern PHP Code Example <?php interface Originator { public function createSnapshot(); } // The originator class class WhiteBoard implements Originator { private $fontSize; private $fontFamily; private $canvasSize; private $canvasColor; private $output; //More fields... function __construct($fontSize, $fontFamily, $canvasSize, $canvasColor, $output) { $this->fontSize = $fontSize; $this->fontFamily = $fontFamily; $this->canvasSize = $canvasSize; $this->canvasColor = $canvasColor; $this->output = $output; } function setFontSize($fontSize) { $this->fontSize = $fontSize; } function setFontFamily($fontFamily) { $this->fontFamily = $fontFamily; } function…

read more