Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Object-georienteerd Programmeren

Object-georienteerd Programmeren

The slides for the OO-training during the Techademy trainingday on June 27th

Stefan Koopmanschap

June 27, 2014
Tweet

More Decks by Stefan Koopmanschap

Other Decks in Programming

Transcript

  1. 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>";
  2. 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);
  3. Constructor <?php class Db { public function __construct($host, $user, $pass,

    $name) { $this->connection = mysql_connect($host, $user, $pass); mysql_select_db($name, $this->connection); } }
  4. Sta$c <?php class Foo { static public function bar() {

    return 'no instance'; } } echo Foo::bar(); // echo's "no instance"
  5. 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); } }
  6. 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 }
  7. 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 }
  8. 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 }
  9. Excep&ons <?php class Db { public function connect($host, $user, $pass)

    { if ($user == '') { throw new InvalidArgumentException('Username may not be empty'); } } }
  10. 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 }