Last active
March 22, 2021 06:39
-
-
Save alexsoyes/6118a5c608b6acca633768a60de4e132 to your computer and use it in GitHub Desktop.
Liskov’s Substitution Principle 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 | |
/** | |
* Liskov’s Substitution Principle (LSP) in PHP | |
*/ | |
abstract class CMS | |
{ | |
abstract public function findArticles(int $limit = 10): array; | |
/** | |
* @throws PDOException | |
*/ | |
public function getConnection(): PDO | |
{ | |
$pdo = new PDO(''); | |
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); | |
return $pdo; | |
} | |
} | |
class WordPress extends CMS | |
{ | |
public function findArticles(int $limit = 10): array | |
{ | |
$pdo = $this->getConnection(); | |
$statement = $pdo->prepare | |
( | |
'SELECT | |
post_title AS title | |
post_content AS content | |
post_date AS createdAt | |
FROM `post` | |
WHERE `post_type` = "post" | |
AND `post_status` = "publish" | |
ORDER BY `post_date` DESC | |
LIMIT :limit' | |
); | |
$statement->bindValue(':limit', $limit, PDO::PARAM_INT); | |
return $statement->fetchAll(); | |
} | |
} | |
class Joomla extends CMS | |
{ | |
public function findArticles(int $limit = 10): array | |
{ | |
$pdo = $this->getConnection(); | |
$statement = $pdo->prepare | |
( | |
'SELECT | |
`title` AS title, | |
`fulltext` AS content, | |
`publish_up` AS createdAt | |
FROM content | |
WHERE `state` = 1 | |
ORDER BY `publish_up` DESC | |
DESC LIMIT :limit' | |
); | |
$statement->bindValue(':limit', $limit, PDO::PARAM_INT); | |
return $statement->fetchAll(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment