Share
Embed
Object-georienteerd Programmeren
The slides for the OO-training during the Techademy trainingday on June 27th
Stefan Koopmanschap
June 27, 2014
Transcript
90 slides
-
Wat$is$OO? <?php mysql_connect('localhost', 'root', 'secret'); mysql_select_db('foo'); echo "<h1>Foo Bar &
Co</h1><br />"; $sql = "SELECT * from bar where category='".$_GET['cat']."'"; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)) { $name = format_name($row['name]); echo $name."<br />"; } echo "<sub>(c) 2014 Foo Bar & Co</sub>"; -
Wat$is$OO? <?php $config = new Config('/path/to/configfile.php'); $request = new Request();
$response = new Response(); $view = new View('/path/to/barlisttemplate.php'); $db = new Db($config->get('dbhost'), $config->get('dbuser'), $config->get('dbpassword'), $config->get('dbname')); $barRepository = new BarRepository($db); $bars = $barRepository->getByCategory($request->get('cat')); $output = $view->parse($bars); $request->send($output); -
Magic&Methods <?php class User { public function __toString() { return
$this->getName(); } } -
Magic&Methods • __construct() • __destruct() • __call() • __callSta1c() •
__get() • __set() • __isset() -
Magic&Methods • __unset() • __sleep() • __wakeup() • __toString() •
__invoke() • _setstate() • __clone() -
Constructor <?php class Db { public function __construct($host, $user, $pass,
$name) { $this->connection = mysql_connect($host, $user, $pass); mysql_select_db($name, $this->connection); } } -
Sta$c <?php class Foo { static public function bar() {
return 'no instance'; } } echo Foo::bar(); // echo's "no instance" -
Access%modifier%(visibility) <?php class Db { private $connection; public function __construct($host,
$user, $pass, $name) { $this->connection = $this->connect($host, $user, $pass); $this->selectDb($name); } protected function connect($host, $user, $pass) { return mysql_connect($host, $user, $pass); } protected function selectDb($name) { mysql_select_db($name, $this->connection); } } -
Encapsula)e Het$encapsuleren$van$de$specifieke$implementa3e$van$een$taak <?php class Db { protected function connect($host, $user,
$pass) { return mysql_connect($host, $user, $pass); } } -
Inheritance <?php class Mysql extends Db { protected function connect($host,
$user, $pass) { return mysql_connect($host, $user, $pass); } // etc } <?php class Postgres extends Db { protected function connect($host, $user, $pass) { return pg_connect("host=".$host." user=".$user." password=".$pass); } // etc } -
Composi'e Het$samenstellen$van$func.onaliteit$door$middel$van$meerdere$ objecten <?php $db = new Db($config->get('dbhost'), $config->get('dbuser'), $config->get('dbpassword'),
$config->get('dbname')); $barRepository = new $barRepository($db); -
Interface Het$maken$van$een$contract$waarnaar$geprogrammeerd$moet$ worden <?php interface DbInterface { public function __construct($host,
$user, $pass, $name); public function query($sql); } class Mysql implements DbInterface { protected function connect($host, $user, $pass) { return mysql_connect($host, $user, $pass); } // etc } -
Abstracte)class Een$abstracte$class$kan$niet$worden$geinstan2eerd$omdat$deze$nog$ niet$volledig$klaar$is <?php abstract class Db { private $connection;
public function __construct($host, $user, $pass, $name) { $this->connection = $this->connect($host, $user, $pass); $this->selectDb($name); } abstract protected function connect($host, $user, $pass); abstract protected function selectDb($name); } class Mysql extends Db implements DbInterface { protected function connect($host, $user, $pass) { return mysql_connect($host, $user, $pass); } // etc } -
Open/Closed+Principle • Iedere&class&moet&open&staan&voor&uitbreiding,&maar&gesloten& voor&aanpassing • Zorg&dat&zo&veel&mogelijk&logica&protected&is,&alleen&hele& specifieke&dingen&private • Gebruik&van&het&"final"&keyword&kan&ook&helpen&voor&(public)& methodes
• Extenden&is&makkelijk • De&code&aanpassen&is&dan&niet&nodig -
Dependency(Inversion(Principle Een$module$dient$geen$harde$a0ankelijkheden$te$hebben$van$ andere$modules • In$PHP$vooral$populair$via$Dependency$Injec5on • Sommige$frameworks$gebruiken$hiervoor$een$ServiceLocator • Zorgt$ook$voor$een$betere$testbaarheid$van$je$code •
Leunt$zwaar$op$het$gebruik$van$interfaces$om$bepaalde$ "contracten"$vast$te$leggen -
Excep&ons <?php class Db { public function connect($host, $user, $pass)
{ if ($user == '') { throw new InvalidArgumentException('Username may not be empty'); } } } -
Excep&ons <?php $db = new Db(); try { $db->connect('localhost', '',
'pass'); } catch (InvalidArgumentException $e) { // handle specifically the invalid argument exceptions } catch (Exception $e) { // handle all other exceptions } -
Namespaces <?php namespace Database\Driver; use Database\BaseDb as Db; class Mysql
extends Db { // etc } -
Hoe$nu$verder? • Best&prac+ces:&Gebruik&design&pa5erns • Refactor&je&eigen&code • Lezen!&Er&zijn&veel&boeken&over&dit&onderwerp • Gebruik&Open&Source:&Veel&frameworks&en&libraries •
Schrijf&zo&veel&mogelijk&tests:&Zorgt&voor&beter&opgeze5e&code