Skip to content

Instantly share code, notes, and snippets.

@joaorobertopb
Created January 4, 2019 20:21
Show Gist options
  • Save joaorobertopb/a0478b9daa7da7bdbdc0d5fb903fea90 to your computer and use it in GitHub Desktop.
Save joaorobertopb/a0478b9daa7da7bdbdc0d5fb903fea90 to your computer and use it in GitHub Desktop.
Exemplos em PHP de violação do princípio de Liskov do SOLID.
<?php
# - Sobrescrevendo um método que não faz nada...
class Voluntario extends ContratoDeTrabalho
{
public function remuneracao()
{
// não faz nada
}
}
# - Lançando uma exceção inesperada...
class MusicPlay
{
public function play($file)
{
// toca a música
}
}
class Mp3MusicPlay extends MusicPlay
{
public function play($file)
{
if (pathinfo($file, PATHINFO_EXTENSION) !== 'mp3') {
throw new Exception;
}
// toca a música
}
}
# - Retornando valores de tipos diferentes...
class Auth
{
public function checkCredentials($login, $password)
{
// faz alguma coisa
return true;
}
}
class AuthApi extends Auth
{
public function checkCredentials($login, $password)
{
// faz alguma coisa
return ['auth' => true, 'status' => 200];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment