Introduction
This article explains the Strategy Pattern in PHP. One of the common issues we encounter while programming is that must create decisions on completely different methods. Strategy Patterns have a common pattern that helps us to create decisions on completely different cases, more simply. To know this is good, let us use the scenario where you are developing a notification program. This notification program can check the given choices for a user. A user might want to be notified in many ways, like email, SMS, or Fax. Your program should check the available choices to contact that user and so create a call upon that. This case will simply be solved by the Strategy Pattern.
Strategy pattern
Example
First of all, I will create an interface.
- <?php
- //interface.confirmation.php
- interface confirmation
- {
- //method name confirm
- public function Confirm();
- }
- ?>
Let's create a "class.Emailconfirmation.php" file.
- <?php
- //include interface.confirm.php
- include_once("interface.confirm.php");
- //create class Emailconfirmation
- class EmailConfirmation implements confirmation
- {
- //method name confirm
- public function confirm()
- {
- //do something to confirm the user by Email
- }
- }
- ?>
And next I will create a "class.faxconfirmation.php" file.
- <?php
- //include class.emailconfirmation.php
- include_once("class.emailconfirmation.php");
- //implements confirmation interface
- class FaxConfirmation implements confirmation
- {
- // create method confimation
- public function confirm()
- {
- //do something to confirm the user by Fax
- }
- }
- ?>

Join the conversation! Your thoughts help the community grow.