Factory Method - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Factory Method.php

  1. <?php
  2.  
  3. abstract class SocialNetworkPoster {
  4.     abstract public function getSocialNetwork(): SocialNetworkConnector;
  5.     
  6.     public function post($content): void {
  7.         $network = $this->getSocialNetwork();
  8.         $network->logIn();
  9.         $network->createPost($content);
  10.         $network->logout();
  11.     }
  12. }
  13.  
  14. class FacebookPoster extends SocialNetworkPoster {
  15.     private $login, $password;
  16.     
  17.     public function __construct(string $login, string $password) {
  18.         $this->login = $login;
  19.         $this->password = $password;
  20.     }
  21.     
  22.     public function getSocialNetwork(): SocialNetworkConnector {
  23.         return new FacebookConnector($this->login, $this->password);
  24.     }
  25. }
  26.  
  27. class LinkedInPoster extends SocialNetworkPoster {
  28.     private $login, $password;
  29.     
  30.     public function __construct($login, $password) {
  31.         $this->login = $login;
  32.         $this->password = $password;
  33.     }
  34.     
  35.     public function getSocialNetwork(): SocialNetworkConnector {
  36.         return new LinkedInConnector($this->login, $this->password);
  37.     }
  38. }
  39.  
  40. interface SocialNetworkConnector {
  41.     public function logIn(): void;
  42.     
  43.     public function logOut(): void;
  44.     
  45.     public function createPost($content): void;
  46. }
  47.  
  48. class FacebookConnector implements SocialNetworkConnector {
  49.     private $login, $password;
  50.     
  51.     public function __construct(string $login, string $password) {
  52.         $this->login = $login;
  53.         $this->password = $password;
  54.     }
  55.     
  56.     public function logIn(): void {
  57.         echo "Sending HTTP API request to log in user $this->login with " . "password $this->password\n";
  58.     }
  59.     
  60.     public function logOut(): void {
  61.         echo "Sending HTTP API request to log out user $this->login\n";   
  62.     }
  63.     
  64.     public function createPost($content): void {
  65.         echo "Sending HTTP API request to create a post in Facebook timeline with the following: '" . $content . "'\n";
  66.     }
  67. }
  68.  
  69. class LinkedInConnector implements SocialNetworkConnector {
  70.     private $email, $password;
  71.     
  72.     public function __construct(string $email, string $password) {
  73.         $this->email = $email;
  74.         $this->password = $password;
  75.     }
  76.     
  77.     public function logIn(): void {
  78.         echo "Sending HTTP API request to login in user $this->email with " . "password $this->password\n";
  79.     }
  80.     
  81.     public function logOut(): void {
  82.         echo "Sending HTTP API request to log out user $this->email\n";
  83.     }
  84.     
  85.     public function createPost($content): void {
  86.         echo "Sending HTTP API request to create a post in LinkedIn timeline with the following: '" . $content . "'\n";
  87.     }
  88. }
  89.  
  90. function clientCode(SocialNetworkPoster $creator) {
  91.     $creator->post("Hello World!");
  92.     $creator->post("I had a large hamburger this morning!");
  93. }
  94.  
  95. echo "Connection 1:\n";
  96. clientCode(new FacebookPoster("john_doe", "***************"));
  97. echo "\n\n";
  98.  
  99. echo "Connection 2:\n";
  100. clientCode(new LinkedInPoster("[email protected]", "******************"));
  101. echo "\n\n";
File Description
  • Factory Method
  • PHP Code
  • 06 Nov-2019
  • 2.91 Kb
You can Share it: