Last active
March 25, 2021 05:02
-
-
Save alexsoyes/0ae520ef3f5d776711604ea22eb77242 to your computer and use it in GitHub Desktop.
Interface Segregation Principle (ISP) in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Interface Segregation Principle (ISP) in PHP | |
* | |
* We have 2 dedicated Interfaces for 2 different needs. | |
*/ | |
interface EntityInterface | |
{ | |
public function getId(): int; | |
public function getName(): string; | |
} | |
interface SerializableInterface | |
{ | |
public function serializeToApi(): string; | |
} | |
/** | |
* Class User | |
* | |
* The class should be displayed in front-end, so getId() and getName() are mandatory. | |
* The class should be sent to a web service, the function serializeToApi() is mandatory. | |
*/ | |
class User implements EntityInterface, SerializableInterface | |
{ | |
private int $id; | |
private int $name; | |
public function serializeToApi(): string | |
{ | |
return json_encode($this); | |
} | |
public function getId(): int | |
{ | |
return $this->id; | |
} | |
public function setId(int $id): void | |
{ | |
$this->id = $id; | |
} | |
public function getName(): int | |
{ | |
return $this->name; | |
} | |
public function setName(int $name): void | |
{ | |
$this->name = $name; | |
} | |
} | |
/** | |
* Class Order | |
* | |
* The class order is only used to display passed orders from the user, no modification allowed. | |
* The class DOES NOT need serializeToApi(). | |
*/ | |
class Order implements EntityInterface | |
{ | |
private int $id; | |
private int $name; | |
public function getId(): int | |
{ | |
return $this->id; | |
} | |
public function getName(): int | |
{ | |
return $this->name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment