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.
Result of php executing
Full code of Factory Method.php
- <?php
- abstract class SocialNetworkPoster {
- abstract public function getSocialNetwork(): SocialNetworkConnector;
- public function post($content): void {
- $network = $this->getSocialNetwork();
- $network->logIn();
- $network->createPost($content);
- $network->logout();
- }
- }
- class FacebookPoster extends SocialNetworkPoster {
- private $login, $password;
- public function __construct(string $login, string $password) {
- $this->login = $login;
- $this->password = $password;
- }
- public function getSocialNetwork(): SocialNetworkConnector {
- return new FacebookConnector($this->login, $this->password);
- }
- }
- class LinkedInPoster extends SocialNetworkPoster {
- private $login, $password;
- public function __construct($login, $password) {
- $this->login = $login;
- $this->password = $password;
- }
- public function getSocialNetwork(): SocialNetworkConnector {
- return new LinkedInConnector($this->login, $this->password);
- }
- }
- interface SocialNetworkConnector {
- public function logIn(): void;
- public function logOut(): void;
- public function createPost($content): void;
- }
- class FacebookConnector implements SocialNetworkConnector {
- private $login, $password;
- public function __construct(string $login, string $password) {
- $this->login = $login;
- $this->password = $password;
- }
- public function logIn(): void {
- echo "Sending HTTP API request to log in user $this->login with " . "password $this->password\n";
- }
- public function logOut(): void {
- echo "Sending HTTP API request to log out user $this->login\n";
- }
- public function createPost($content): void {
- echo "Sending HTTP API request to create a post in Facebook timeline with the following: '" . $content . "'\n";
- }
- }
- class LinkedInConnector implements SocialNetworkConnector {
- private $email, $password;
- public function __construct(string $email, string $password) {
- $this->email = $email;
- $this->password = $password;
- }
- public function logIn(): void {
- echo "Sending HTTP API request to login in user $this->email with " . "password $this->password\n";
- }
- public function logOut(): void {
- echo "Sending HTTP API request to log out user $this->email\n";
- }
- public function createPost($content): void {
- echo "Sending HTTP API request to create a post in LinkedIn timeline with the following: '" . $content . "'\n";
- }
- }
- function clientCode(SocialNetworkPoster $creator) {
- $creator->post("Hello World!");
- $creator->post("I had a large hamburger this morning!");
- }
- echo "Connection 1:\n";
- clientCode(new FacebookPoster("john_doe", "***************"));
- echo "\n\n";
- echo "Connection 2:\n";
- echo "\n\n";
File Description
- Factory Method
- PHP Code
- 06 Nov-2019
- 2.91 Kb
You can Share it:
Latest PHP Pastes