Your SlideShare is downloading. ×
0
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Decoupling Objects With Standard Interfaces

313

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
313
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
8
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. IPC Spring 2013IPC Spring 2013Decoupling Objects WithStandard Interfaces
  • 2. About MeAbout Me● Thomas Weinert● papaya Software GmbH● papaya CMS● Twitter: @ThomasWeinert● Web: http://www.a-basketful-of-papayas.net
  • 3. InterfacesInterfaces● By Declaration● By Convention● Magic Methods
  • 4. ReasonsReasons● Encapsulation● Reuse● S.O.L.I.D.
  • 5. ImplementImplementclass FooimplementsCountable,IteratorAggregate {}
  • 6. ExtendExtendinterface Foo extends IteratorAggregate {}
  • 7. ValidateValidateif ($foo instanceOf Traversable) {}
  • 8. Type HintsType Hintsfunction bar(Traversable $foo) {}
  • 9. InterfacesInterfacesBy Convention
  • 10. Object InitializationObject Initialization● Magic Methods● __construct()● __destruct()● __clone()● __sleep()/__wakeup() → Serializeable● __set_state()
  • 11. __toString__toString● Cast an object to string/*** Casting the object to string will*return the last value* @return string*/public function __toString() {return (string)end($this->_values);}
  • 12. Dynamic PropertiesDynamic Properties● $object->value = 123● $value = $object->value;● $object->setValue(123);● $value = $object->getValue();
  • 13. Dynamic PropertiesDynamic Properties● __isset()● __get()● __set()● __unset()
  • 14. __isset()__isset()/*** @param string $name* @return boolean*/public function __isset($name) {switch ($name) {case name :case value :case values :return TRUE;}return FALSE;}
  • 15. __get()__get()/*** @param string $name* @throws InvalidArgumentException* @return mixed*/public function __get($name) {switch ($name) {case name :return $this->_name;case value :return end($this->_values);case values :return $this->_values;}throw new LogicException(sprintf(Can not read non existing property: %s::$%s,__CLASS__,$name));}
  • 16. __set()__set()/*** @param string $name* @param string|array|Traversable $value* @throws LogicException*/public function __set($name, $value) {switch ($name) {case name :$this->setName($value);return;case value :$this->setData((string)$value);return;case values :$this->setData($value);return;}throw new LogicException(sprintf(Can not write non existing property: %s::$%s,__CLASS__, $name...
  • 17. __call/__callStatic__call/__callStaticDynamic Methods/Functions
  • 18. Generic CallbacksGeneric Callbacksclass MyEvents extends Events {public function __construct() {parent::__construct([example => 21]);}}$my = new MyEvents();echo $my->onExample(), "n"; //21$my->onExample = function () {return 42;};echo $my->onExample(), "n"; //42$my->onExample = 23;echo $my->onExample(), "n"; //23
  • 19. PHP DocumentorPHP Documentor/** …** @property Callable $onExample* @method mixed onExample()*/class MyEvents extends Events {public function __construct() {parent::__construct([example => 21]);}}
  • 20. CallableCallable● function● array($object, method)● function() {}● class {}
  • 21. __invoke__invoke● Functors● Callable
  • 22. Callback ListsCallback Lists...$queries = IoDeferred::When($mysqlOne("SELECT Query 1, SLEEP(5)")->done(function($result) use ($time) {var_dump(iterator_to_array($result));var_dump(microtime(TRUE) - $time);}),$mysqlTwo("SELECT Query 2, SLEEP(1)")->done(function($result) use ($time) {var_dump(iterator_to_array($result));var_dump(microtime(TRUE) - $time);}));
  • 23. Functor ExampleFunctor Exampleclass DatabaseResultDebugger {private $_startTime = 0;public function __construct($startTime) {$this->_startTime = $startTime;}public function __invoke($result) {var_dump(iterator_to_array($result));var_dump(microtime(TRUE) - $this->_startTime);}}
  • 24. Callback HandlersCallback Handlers$queries = IoDeferred::When($mysqlOne("SELECT Query 1, SLEEP(5)")->done(new DatabaseResultDebugger($time)),$mysqlTwo("SELECT Query 2, SLEEP(1)")->done(new DatabaseResultDebugger($time)));
  • 25. More Callback HandlersMore Callback Handlers$queries = IoDeferred::When($mysqlOne("SELECT Query 1, SLEEP(5)")->done(new ConcreteDatabaseResultHandler())->done(new DatabaseResultDebugger($time)),$mysqlTwo("SELECT Query 2, SLEEP(1)")->done(new DatabaseResultDebugger($time)));
  • 26. Predeclared Interfaces
  • 27. SerializeableSerializeable● Usage● serialize($object)● unserialize($string);● Methods● $object->serialize()● $object->unserialize($string);
  • 28. JsonSerializableJsonSerializable● Usage● json_encode()● Method● $object->jsonSerialize()
  • 29. ArrayAccessArrayAccess● [] -Syntax● Validation● Conversion● Lazy Initialization
  • 30. ArrayAccess MethodsArrayAccess Methods● offsetExists()● offsetGet()● offsetSet()● offsetUnset()
  • 31. OffsetExists()OffsetExists()/*** @param integer $offset* @return boolean*/public function offsetExists($offset) {return array_key_exists($offset, $this->_values);}
  • 32. OffsetGet()OffsetGet()/*** @param integer $offset*/public function offsetGet($offset) {return $this->_values[$offset];}
  • 33. OffsetSet()OffsetSet()/*** @param integer $offset* @param string $value*/public function offsetSet($offset, $value) {if (NULL === $offset) {$this->_values[] = $value;} else {$this->_values[$offset] = $value;}}
  • 34. OffsetUnset()OffsetUnset()/*** @param integer $offset*/public function offsetUnset($offset) {unset($this->_values[$offset]);}
  • 35. Array DereferencingArray Dereferencingecho $object->foo()[0];
  • 36. CountableCountable● count($foo)class DatabaseResult implements Countable {...public function count() {return $this->_count;}...}
  • 37. TraversableTraversable● foreach● Iterator● IteratorAggregate
  • 38. Iterator InterfacesIterator InterfacesTraversableIteratorOuterIteratorIteratorAggregateRecursiveIteratorSeekableIteratorSPL
  • 39. Iterator functionsIterator functions● foreach()● iterator_to_array()● iterator_count()● iterator_apply()
  • 40. IteratorIterator● rewind()● next()● valid()● current()● key()
  • 41. IteratorAggregateIteratorAggregate● getIterator()class Foo implements IteratorAggregate {private $_items = array();public function __construct(array $items) {$this->_items = $items;}public function getIterator() {return new ArrayIterator($this->_items);}}
  • 42. Predefined IteratorsPredefined Iterators● ArrayIterator● EmptyIterator● RegexIterator● …
  • 43. Nest IteratorsNest Iterators● IteratorIterator● AppendIterator● CallbackFilterIterator● ...
  • 44. RecursiveIteratorRecursiveIterator● List with Children$items = [1 => 1,2 => 1.1,3 => 2];$structure = [0 => [1, 3],1 => [2]];
  • 45. Inherit from IteratorIteratorInherit from IteratorIteratorclass Barextends IteratorIteratorimplements RecursiveIterator {private $_items = [];private $_structure = [];public function __construct($items, $structure, $parent = 0) {$siblings = [];if (!empty($structure[$parent])) {foreach ($structure[$parent] as $id) {if (isset($items[$id])) {$siblings[$id] = $items[$id];}}}parent::__construct(new ArrayIterator($siblings));$this->_items = $items;$this->_structure = $structure;}...
  • 46. hasChildren()hasChildren()public function hasChildren() {return$this->valid() &&isset($this->_structure[$this->key()]);}
  • 47. getChildren()getChildren()public function getChildren() {if ($this->hasChildren()) {return new $this($this->_items,$this->_structure,$this->key());} else {return new $this([], []);}}
  • 48. RecursiveIteratorIteratorRecursiveIteratorIterator● Iterator for RecursiveIterator$iterator = new RecursiveIteratorIterator(new Bar($items, $structure),RecursiveIteratorIterator::SELF_FIRST);foreach ($iterator as $element) {echo str_repeat(*, $iterator->getDepth() + 1);echo $element;echo "n";}
  • 49. ResultResult* 1** 1.1* 2● No recursive function call● Works with Traversable/ArrayAccess● Allows for Lazy Initalization
  • 50. ObserverObserverSubjectObserver 2 Observer 2Observer 2
  • 51. ObserverObserverCMS Page publishedLog Action Announce ToWebserviceInvalidateCache
  • 52. ObserverObserver● SplObserver● update()● SplSubject● attach()● detach()● notify()
  • 53. Thank YouThank You● Twitter: @ThomasWeinert● https://joind.in/8807

×