You have a basic OOP knowledge in PHP? You have seen the usual examples, but don't really see how to apply them to solve your coding problems? This session will show you how you can make better use of OOP by presenting seven best practices and teach you how to apply them in real life. Topics covered include dependency injection, separation of concerns, loose coupling, and much more.
5. „
„Hang the rules.
Hang the rules.
They're more like
They're more like
guidelines anyway.“
guidelines anyway.“
--Elizabeth Swann,
--Elizabeth Swann,
Pirates of the Caribbean
Pirates of the Caribbean
15. class Something
{
public function doWork()
{
… load data …
… perform calculations …
… render HTML output …
… store calculation result ...
}
}
16. class Something
{
public function doManyThings()
{
$this->loadData();
$this->performCalculations();
$this->renderHtml();
$this->storeResult();
}
protected function loadData()
protected function performCalulations()
protected function renderHtml()
protected function storeResult()
}
17. class DataLoader
{
public function loadData()
}
class Calculator
{
public function calculateResult()
}
class HtmlRenderer
{
public function render()
}
class StorageManager
{
public function storeResult()
}
28. class Person
{
protected function talkTo(Friend $friend)
{
$friend->askForACigarette();
}
}
class Friend
{
public function askForACigarette()
{
return new Cigarette();
}
}
47. class Car
{
protected $engine;
protected $steeringWheel;
protected $frontSeat;
public function __construct()
{
$this->engine = new Engine();
$this->steeringWheel = new SteeringWheel();
$this->frontSeat = new Seat();
}
}
48. class Car
{
public function __construct(Engine $engine,
SteeringWheel $steeringWheel,
Seat $seat)
{
$this->engine = $engine;
$this->steeringWheel = $steeringWheel;
$this->frontSeat = $seat;
}
}
49. $engine = new Engine();
$steeringWheel = new SteeringWheel();
$seat = new Seat();
$car = new Car($engine, $steeringWheel, $seat);
50. $engine = new SuperStrongEngine();
$steeringWheel = new FancySteeringWheel();
$seat = new MichaelSchuhmacherRacingSeat();
$car = new Car($engine, $steeringWheel, $seat);