4. Schlüsselwörter
● class ● interface
● const ● abstract
● function ● Final
● public ● extends
● protected ● implements
● private
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. 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. 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. 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
)
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. 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. 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. 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
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. 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. 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);
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. 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. 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. 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"