PHP OOP

2,580 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,580
On SlideShare
0
From Embeds
0
Number of Embeds
20
Actions
Shares
0
Downloads
107
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

PHP OOP

  1. 1. PHP OOP
  2. 2. Übersicht ● Basics ● Exceptions ● Magic Methods ● Dependency Injection ● PHP 5.3
  3. 3. Basics
  4. 4. Schlüsselwörter ● class ● interface ● const ● abstract ● function ● Final ● public ● extends ● protected ● implements ● private
  5. 5. Interface interface MyInterface { ● Kann Methoden, } public function output1($text); Constanten enthalten interface MyInterface2 extends MyInterface { const DELIMITER = PHP_EOL; ● public } nur Signatur keine Logik class MyClass implements MyInterface2 { ● public function output1($text) { echo $text . self::DELIMITER; ● können Interfaces } } erweitern echo 'A' . MyInterface2::DELIMITER; Klassen Signatur muss $class = new MyClass(); ● $class->output1('test'); exakt mit Interface übereinstimmen A test
  6. 6. Abstract abstract class AbstractClass { ● Klassen, Methoden abstract protected function getValue(); abstract protected function prefixValue($prefix); public function printOut() { ● Kann nicht instanziiert } print $this->getValue () . "n"; werden } class ConcreteClass1 extends AbstractClass { protected function getValue() { ● Kann Methoden, } return "ConcreteClass1"; Variablen, public function prefixValue($prefix) { return "{$prefix}ConcreteClass1"; } Constanten enthalten } $class1 = new ConcreteClass1(); ● Sichtbarkeit kann $class1->printOut(); echo $class1->prefixValue('FOO_'); aufgeweicht aber ConcreteClass1 nicht verstärkt werden FOO_ConcreteClass1
  7. 7. Final class MyClass { ● Methode final public function getClassName() { return __CLASS__; } ● Kann nicht } überschrieben werden final class MyClassFinal extends MyClass { public function getValue() { return 'Gordon'; ● Klasse } } ● Kann nicht mehr erweitert werden
  8. 8. Type Hinting class MyClass ● Objects { public function test(OtherClass $otherclass) { echo $otherclass->var . PHP_EOL; ● Object } public function test_array(array $input_array) { print_r($input_array); ● Spezial object } } ● array class OtherClass { public $var = 'Hallo Welt'; } ● keine weiteren $class = new MyClass(); $class ->test(new OtherClass()); $class->test_array(array(1, 'Gordon')); Hallo Welt Array ( [0] => 1 [1] => Gordon )
  9. 9. Exceptions
  10. 10. Exception class MyException extends Exception { ● Ausnahmebehandlung } class Test { ● Catch fängt auch public function testing() { try { parent Exceptions try { throw new MyException('foo!'); } catch (MyException $e) { Kann verschachtelt throw $e; ● } } catch (Exception $e) { werden var_dump($e->getMessage(),$e->getCode(), $e->getFile(),$e->getLine()); } } } $foo = new Test(); $foo->testing(); string(4) "foo!" int(0) string(42) "/home/gfranke/workspace/test/exception.php" int(12)
  11. 11. Magic Methods
  12. 12. Construct & Destruct class MyClass { ● Initialisieren und public function __construct() { echo '__construct' . PHP_EOL; löschen } public function __destruct() { echo '__destruct' . PHP_EOL; } } $class = new MyClass(); echo 'do something' . PHP_EOL; unset($class); __construct do something __destruct
  13. 13. To String class MyClass { ● Aufruf bei einen public function __toString() { return 'Gordon' . PHP_EOL; String cast auf ein } } Object $class = new MyClass(); echo $class; echo (string) $class; echo $class->__toString(); Gordon Gordon Gordon
  14. 14. Set & Get class MyClass { ● Nur bei nicht public function __set($name, $value) { echo '__set' . PHP_EOL; existierende } $this->$name = $value; Eigenschaften public function __get($name) { return 'not exist'; } } ● Unkontrolliertes $class = new MyClass(); setzen von var_dump($class->name); $class->name = 'Peter'; var_dump($class->name); Eigenschaften $class->name = 'Marco'; var_dump($class->name); verhindern string(9) "not exist" __set string(5) "Peter" string(5) "Marco"
  15. 15. Call & Call Static Wenn Funktion nicht class MyClass { ● public function __call($name, $arguments) { echo '__call' . PHP_EOL; existiert oder nicht echo $this->getName(); } static public function __callStatic($name, $arguments) { sichtbar ist } echo '__callStatic' . PHP_EOL; echo self::getName(); static protected function getName() { ● __callStatic erst ab } } return 'Gordon'; PHP 5.3 $class = new MyClass(); $class->getName2(); $class->getName(); MyClass::getName2(); __call Gordon __call Gordon __callStatic Gordon
  16. 16. Weitere ● __isset ● __toString ● __unset ● __invoke (5.3) ● __sleep ● __set_state ● __wakeup ● __clone
  17. 17. Dependency Injection
  18. 18. Problem class MyCounter { ● Macht mehr als die public function __construct() { $this->memcache = new Memcache(); Klasse soll } $this->memcache->connect('localhost:11211'); public function setCounter($counter) { Schlecht testbar $this->memcache->set('counter', $counter); ● } } ● Memcacheinstanz $class = new MyCounter(); $class->setCounter(4); muss laufen
  19. 19. Vereinfachen class MyCounter { ● Memcacheinstanz public function __construct($cache) { $this ->cache = $cache; injizieren } public function setCounter($counter) { $this->cache->set('counter', $counter); } } $cache = new Memcache(); $cache->connect('localhost:11211'); $class = new MyCounter($cache); $class->setCounter(4);
  20. 20. Testbar machen interface MyIStorage { ● Memcache Kapseln } public function set($name, $value); class MyStorageMemcache implements MyIStorage { public function __construct($memcache) { $this->cache = $memcache; } public function set($name, $value) { $this->cache->set($name, $value); } } class MyCounter { public function __construct(MyIStorage $cache) { $this->cache = $cache; } public function setCounter($counter) { $this->cache->set('counter', $counter); } } $memcache = new Memcache(); $memcache->connect('localhost:11211'); $cache = new MyStorageMemcache($memcache); $class = new MyCounter($cache); $class->setCounter(4);
  21. 21. PHP 5.3
  22. 22. Neues I class MyClass { ● Heredoc für static $varStatic = 'Gordon'; public $var = <<<DATA Eigenschaften Hello World! DATA; Variable als } ● $className = 'MyClass'; Klassenreferenz $class = new $className(); echo $class->var; echo $className::$varStatic . PHP_EOL; Hello World! Gordon
  23. 23. Neues II class MyClass { ● __destruct Exception public function __destruct() { throw new Exception('No Fatal Error'); Kein Fatal Error } public function __invoke($x) { var_dump($x); __invoke() } ● } __callStatic() $class = new MyClass(); $class(5); try { unset($class); } catch(Exception $e) { echo $e->getMessage() . PHP_EOL; } int(5) No Fatal Error
  24. 24. Late Static Bindings class A { ● Schlüsselwort static public static function who() { echo __CLASS__ . PHP_EOL; } ● ruft die Funktion in public static function test() { self ::who(); der Kindklasse auf } public static function testLateStaticBinding() { static::who(); // Here comes Late Static Bindings } } class B extends A { public static function who() { echo __CLASS__ . PHP_EOL; } } B::test(); B::testLateStaticBinding(); A B
  25. 25. Namespaces namespace A; ● Namespace ini_set('error_reporting', E_ALL); ● immer am Anfang der Datei ini_set('display_errors', true); ● Benutzen function str_repeat($input, $multiplier = 2) { return str_repeat($input . ' ', $multiplier); } ● Use A; new MyClass(); var_dump(str_repeat('Gordon')); var_dump(Astr_repeat('Gordon', 3)); ● new AMyClass(); var_dump(str_repeat('Gordon', 3)); ● spezielle Konstanten können nicht überschrieben werden ● NULL, TRUE, FALSE, ZEND_THREAD_SAFE und ZEND_DEBUG_BUILD string(14) "Gordon Gordon " string(21) "Gordon Gordon Gordon " string(18) "GordonGordonGordon"
  26. 26. Danke

×