Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Spl in the wild - zendcon2012


Published on

Published in: Technology
  • Be the first to comment

Spl in the wild - zendcon2012

  1. 1. SPL IN THE WILDThe Good, the Bad and the Ugly
  2. 2. NOT A BEGINNERS TALK!If you don’t know SPL basics, this talk might go fast for you.Please rate me –
  3. 3. Why SPL?What SPL?How SPL? A library of standard interfaces, classes, and functions designed to solve common programming problems and allow engine overloading
  4. 4. BUT ISN’T SPL AN EXTENSION? SPL is an extension SPL is a core extension SPL cannot be built shared SPL should not be turned off SPL is present in PHP since 5.0 (almost 5 years ago) As of 5.3, SPL cannot be turned off If you don’t have SPL, whoever built your PHP is an idiot.
  5. 5. Don’t let friends use __autoloadAUTOLOAD
  6. 6. AUTOLOAD STACK ERR QUEUEspl_autoload_register()spl_autoload_unregister()spl_autoload_call()spl_autoload_functions()
  7. 7. BEWARE THE WEIRDNESSspl_autoload() lowercase relative paths namespace awarespl_autoload_extensions() include the . no spaces in between comma separated string
  8. 8. IN THE WILDVanilla Forums   b/23d46ffec12624738fabb4d1917178b4a78 72ffd/library/core/functions.general.php#L83
  9. 9. What went wrong whereEXCEPTIONS
  10. 10. EXCEPTION CLASSES LogicExceptionBadFunctionCall InvalidArgument Domain Length OutofRange Exception Exception Exception Exception ExceptionBadMethodCall Exception
  11. 11. EXCEPTION CLASSES RuntimeException UnexpecteOutofBounds Overflow Range Underflow dValue Exception Exception Exception Exception Exception
  12. 12. IN THE WILDZF2 
  13. 13. Yes PHP has a file object FILES
  14. 14. SPLFILEINFO Metadata about a file Returned by directory iterators Can set your own superclass for splfileinfo/splfileobject In the Wild  phpcmc  ts/phpcmc/micro/PhpLinterTest.php#L40
  15. 15. SPLFILEOBJECT Open file pointer Extends splfileinfoIn the Wild Utils Plugin for CakePHP  Model/Behavior/CsvImportBehavior.php#L68
  16. 16. Magic objects oh myARRAYS AS OBJECTS
  17. 17. SPECIFICALLY ARRAYOBJECT Implements ArrayAccess (with references) Implements Countable Implements IteratorAggregate Implements Serializable (since 5.3.0) And other methods that arrays can use (but not all) In the Wild  Rapide Framework  framework/blob/master/lib/Rapide/Utility/ArrayObject.php# L13
  18. 18. Magic and organizationINTERFACES
  19. 19. QUICK TANGENTTraversable  (none)Iterator  Framework/blob/11c4c550bf95bed9b142efa46200c 94330350105/TPostList.php#L9IteratorAggregate  b81e0351a45d98eb1506134e6b56/lib/Pheasant/Ide ntity.php#L5
  20. 20. MORE CORE INTERFACESArrayAccess  129cad529d/src/Symfony/Component/Form/Form.php#L748Serializable  hp#L276JsonSerializable Closure  Do not use (seriously)
  21. 21. SPL - COUNTABLE Interface meaning “you can count me” Can be put on any class Makes count() magical Note this is NOT the same as iterator_count() redis/blob/master/lib/Redis/Servers.php#L9
  22. 22. ITERATOR INTERFACES Outer Iterator Inner Iterator Seekable Iterator For examples, we’ll look at the iterator classes
  23. 23. SPLSUBJECT SPLOBSERVER Are you implementing the observer pattern in your code? Do you intend to have other people use your code/library in some way? Are you implementing something LIKE the observer pattern? In the Wild  EmailLabs_Sync  y/EmailLabs/Logger.php#L93   13d5d1f69f/src/VIH/ExceptionHandler.php#L40
  24. 24. New ways of managing dataDATASTRUCTURES
  25. 25. DOUBLYLINKEDLISTS – CSLESSON ordered collection of values linked to each element before it linked to each element after it “doubly linked” PHP datastructure – a php object with a doublylinkedlist stored inside it
  26. 26. SPLDOUBLYLINKEDLIST Don’t use this Yes, that’s a terrible thing to say – but this is really nothing more then a “base class” with little to recommend on its own Has a doublylinkedlist from C underneath instead of a hashtable – if you know what that means you may find a real use for this (I have not) n/Dispatcher.php#L111
  27. 27. SPLSTACK Data is in LIFO Anything you need to iterate a lot Even cooler? Turn on the mode that will autodelete each item as you process it Any Queue you need to push stuff onto and deal with in LIFO order hp#L54
  28. 28. SPLQUEUE Data is in FIFO Anything you need to iterate a lot Even cooler? Turn on the mode that will autodelete each item as you process it Any Queue you need to push stuff onto and deal with in LIFO order xThreadPool.php#L28
  29. 29. HEAP – QUICK CS LESSON comparison function used to compare the new element to other elements element is placed according to functions return value underlying algorithm does it with minimal comparisons PHP datastructure – a php object with a heap stored inside it
  30. 30. USING SPLHEAP This is an abstract class You need to compare elements alsh/LibSprite/Util/Block/Heap/Width.php#L15 alsh/LibSprite/Packer/GreedyHeap.php#L16
  31. 31. SPLMINHEAP, SPLMAXHEAP These are concrete heap implementations, designed to grab the lowest possible value out, or the highest possible snippets/blob/master/datastructures/bottom_k.php#L44 in-PHP/blob/master/AStarWithHeap.php#L196
  32. 32. SPLPRIORITYQUEUE Uses heap internally Is non-deterministic when identical priorities are used ueue.php#L16
  33. 33. SPLFIXEDARRAY You have a large amount of data, you know the final size, you need to stick it into an array You’re not going to expand it past the final size This is not a small amount of data You might need it in non-sequential order but can handle having only integers for keys r/Calendar.php#L83
  34. 34. SPLOBJECTSTORAGEThis can be used two ways Objects can be keys in an array (with two values) As a Set (with one value) er/QueryPath/CssEventHandler.php#L17
  35. 35. Take a Drink …ITERATORS
  36. 36. (RECURSIVE)FILTERITERATOR Abstract Class Has one method that must be implemented – accept – which should return true or false Highly useful for many types of iteration udeFilter.php PTools/Namespacer/RecursiveFilterIterator.php FilterIterator OuterIterator Iterator Traversable
  37. 37. (RECURSIVE)ITERATORITERATOR Regular Class Stick in something that implements traversable Instant Iterator nsformIterator.php mponent/Finder/Iterator/DepthRangeFilterIterator.php IteratorIterator OuterIterator Iterator Traversable
  38. 38. (RECURSIVE)ARRAYITERATOR Regular Class Iterates an array – OR the public properties of an object! (neat trick – dirty trick) Diggin/Service/Wedata/Items.php Validation/ExceptionIterator.php ArrayAccessArrayIter Seekabl Traversabl and Iterator e Countable ator eIterator too!
  39. 39. APPENDITERATOR Keep stacking more iterators on the end with append Utils/Finder.php ParentIterator OuterIterator Iterator Traversable
  40. 40. LIMITITERATOR Regular Class Like mysql’s limit – pick your range and offset and foreach away! nentTreePanel.phpp#L187 LimitIterator OuterIterator Iterator Traversable
  41. 41. FILESYSTEMITERATOR Extends directory iterator Lets you choose how to get data (just string names possible) git/blob/d502926d5b22a4081a9efffd60dbb1b8094908c0/library/Gi t/AdapterAbstract.php FileSystemIterator DirectoryIterator Iterator Traversable
  42. 42. (RECURSIVE)CACHINGITERATOR  Regular Class  Manages another iterator by checking whether it has more elements each time using a hasNext() method  Nashville/blob/master/plugins/sfPhpExcelPlugin/lib/PHPExcel/PH PExcel/Worksheet/RowIterator.php CachingIterator OuterIterator Iterator TraversableRecursiveCa CachingIterator OuterIterator Iterator TraversablechingIterator
  43. 43. (RECURSIVE)DIRECTORYITERATOR Regular Class Walk a Directory php#316 Framework/blob/master/libs/Smarty/sysplugins/smarty_internal_ut ility.php#66 DirectoryIterator SplFIieInfo Iterator Traversable RecursiveDirectoryIteerator DirectoryIterator
  44. 44. RECURSIVETREEITERATOR Regular Class Can create an ascii graphic tree (seriously…) Validation/Exceptions/AbstractNestedException.php#58RecursiveT RecursiveIter OuterIterator Iterator TraversablereeIterator atorIterator
  45. 45. (RECURSIVE)REGEXITERATOR Regular Class Filter an iterator by a regex Pick how you want it to match core/blob/master/lib/utilities/Vcms-FileUtils.php#L144 RegexIterator FilterIterator Iterator Traversable RecursiveRegexIteerator RegexIterator
  46. 46. EMPTY AND INFINITE ITERATORS In the Wild – EmptyIterator  apper.class.php#L680 Infinite? Really only useful for testing EmptyIterator Iterator Traversable InfiniteIterator IteratorIterator Iterator Traversable
  47. 47. MORE EXIST ParentIterator NoRewindIterator MultipleIterator GlobIterator CallbackFilterIterator RecursiveCallbackFilterIterator
  48. 48. WHAT DO YOU WANT TO SEE INSPL?More standard interface?More datastructures? trees? graphs?More iterators? really? more?
  49. 49. GET INVOLVEDBlog postsArticlesUse SPL in the wild
  50. 50. CONTACT ME IRC – freenode – auroraeosrose #coapp and others