interface NotifyInterface { public function notify(); } class SMSNotification implements NotifyInterface { public function notify() { // Code to send SMS notification } } class EmailNotification implements NotifyInterface { public function notify() { // Code to send an email notification } } class NotificationService { public function sendNotification(NotifyInterface $notifier) { $notifier->notify(); } } $message = "Notification message"; $service = new NotificationService(); $email = new EmailNotification(); $service->sendNotification($email);
class PluginManager { protected $plugins = array(); public function register(PluginInterface $plugin) { $this->plugins[] = $plugin; } public function run() { foreach ($this->plugins as $plugin) { $plugin->run(); } } } interface PluginInterface { public function run(); } class Plugin1 implements PluginInterface { public function run() { echo "Plugin1 is running\n"; } } class Plugin2 implements PluginInterface { public function run() { echo "Plugin2 is running\n"; } } $manager = new PluginManager(); $manager->register(new Plugin1()); $manager->register(new Plugin2()); $manager->run();In this example, we define a PluginManager class that can be used to manage a set of plugins. Plugins can be registered with the manager using the register() method, which takes an implementation of the PluginInterface. The run() method of the manager calls the run() method of all registered plugins. We also define two implementations of PluginInterface, Plugin1, and Plugin2, and register them with the manager before calling the run() method. Overall, PHP OCP Util is a package library that provides utilities to implement the Open Closed Principle in PHP code. It contains classes and methods that make it easy to build software components that are open to extension but closed to modification, thereby improving code maintainability and reusability.