@0livier, Full stack (dev|dad|CEO of catchy.io)
class House {
public function __construct() {
$this->door = new Door;
$this->window = new Window;
}
}
function setLogger(Monolog\Logger $logger) {...}
vs
function setLogger(Psr\Log\LoggerInterface $logger) {...}
Dependant on context (volume, usage frequency...)
What the heck ?
strpbrk
, strcspn
function createLogEntry($action, $object, LoggableAdapter $ea)
Duh
5 principles found by Robert C. Martin
A class should have one, and only one, reason to change.
class Book {
function getTitle() {
return "A Great Book";
}
function getAuthor() {
return "John Doe";
}
function turnPage() {
// pointer to next page
}
function getCurrentPage() {
return "current page content";
}
function save() {
$filename = '/documents/'. $this->getTitle(). ' - ' . $this->getAuthor();
file_put_contents($filename, serialize($this));
}
}
Objects or entities should be open for extension, but closed for modification.It's funny, I opened 5 pages on that aspect and found 5 different interpretations. Maybe another one here :p
Especially useful for libraries, rigidify your code
final
keyword with cautiondocblock
commentsDerived classes must be substitutable for their base classes.
class Square extends Rectangle {
public function setHeight($value) {
$this->width = $value;
$this->height = $value;
}
public function setWidth($value) {
$this->width = $value;
$this->height = $value;
}
}
Make fine grained interfaces that are client specific.
interface Vehicule {
public function getSpeed();
public function getMotor();
}
interface Vehicule {
public function getSpeed();
}
interface MotorizedVehicule {
public function getMotor();
}
class Bicycle implements Vehicule {...}
class Car implements Vehicule, MotorizedVehicule {...}
Depend on abstractions, not on concretions.
function setLogger(Psr\Log\LoggerInterface $logger) {...}
Singleton Tightly coupled code Untested stuff Premature optimization Indescriptive naming Duplicated code |
Single responsibility principle Open/closed principle Liskov substitution Interface segregation Dependency inversion principle |