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.
  1. <?php
  2. //interface.confirmation.php
  3. interface confirmation
  4. {
  5. //method name confirm
  6. public function Confirm();
  7. }
  8. ?>
Let's create a "class.Emailconfirmation.php" file.
  1. <?php
  2. //include interface.confirm.php
  3. include_once("interface.confirm.php");
  4. //create class Emailconfirmation
  5. class EmailConfirmation implements confirmation
  6. {
  7. //method name confirm
  8. public function confirm()
  9. {
  10. //do something to confirm the user by Email
  11. }
  12. }
  13. ?>
And next I will create a "class.faxconfirmation.php" file.
  1. <?php
  2. //include class.emailconfirmation.php
  3. include_once("class.emailconfirmation.php");
  4. //implements confirmation interface
  5. class FaxConfirmation implements confirmation
  6. {
  7. // create method confimation
  8. public function confirm()
  9. {
  10. //do something to confirm the user by Fax
  11. }
  12. }
  13. ?>
And next I will create a "class.SMSconfirmation.php" file.
  1. <?php
  2. //include class.faxconfirmation.php
  3. include_once("class.faxconfirmation.php");
  4. //implements confirmation interface
  5. class SMSConfirmation implements confirmation
  6. {
  7. public function confirm()
  8. {
  9. //do something to confirm the user by SMS
  10. }
  11. }
  12. ?>
Now I will use this code:
  1. <?php
  2. //include three files
  3. include_once("class.Emailconfirmation.php");
  4. include_once("class.Faxconfirmation.php");
  5. include_once("class.SMSconfirmation.php");
  6. //user object
  7. $user = new User();
  8. $confirmation = $user->getconfirmation();
  9. switch ($confirmation)
  10. {
  11. case "email":
  12. //object of Emailconfirmation class
  13. $objconfirmation = new Emailconfirmation();
  14. break;
  15. case "sms":
  16. //object of SMSconfirmation class
  17. $objconfirmation = new SMSconfirmation();
  18. break;
  19. case "fax":
  20. //object of Faxconfirmation class
  21. $objconfirmation = new Faxconfirmation();
  22. break;
  23. }
  24. $objconfirmation->confirm();
  25. ?>
In the code above I have used three classes known as "SMSconfirmation", "Emailconfirmation", and "Faxconfirmation". Of these classes implement the confirmation interface, that includes a method named notify. Every one of those categories implements that method on their own.