An Introduction to Object-Oriented Programming, Design Patterns, and Test-Driven Development

C9a5478931da1bbdcc0be2800daa2915?s=47 Jack Lenox
September 06, 2016

An Introduction to Object-Oriented Programming, Design Patterns, and Test-Driven Development

A talk given at WordCamp Singapore 2016. Notes and resources available on GitHub here: https://github.com/jacklenox/introduction-to-oop

C9a5478931da1bbdcc0be2800daa2915?s=128

Jack Lenox

September 06, 2016
Tweet

Transcript

  1. An Introduction... ...to Object­Oriented Programming, Design Patterns, and Test­Driven Development

    By Jack Lenox ( WordPress.com VIP )
  2. Slides are available at: github.com/jacklenox/introduction­to­oop

  3. Introducing the introduction What is Object­Oriented Programming?

  4. "Object­oriented programming (OOP) is a programming paradigm based on the

    concept of "objects", which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods." Wikipedia
  5. None
  6. None
  7. C++ Delphi Objective­C Java C# .NET Ruby

  8. PHP 5 (July 2006)

  9. WordPress Est. 2003

  10. So, what is not OOP?

  11. "Normal" PHP tends to come under the banner of Procedural

    Programming
  12. Procedural Programming Linear Written in the order it's executed Not

    necessarily modular
  13. Procedural Form Submission < ? p h p i f

    ( i s s e t ( $ _ P O S T [ ' n a m e ' ] ) ) { $ n a m e = s a n i t i z e _ t e x t _ f i e l d ( $ _ P O S T [ ' n a m e ' ] ) ; i f ( s t r l e n ( $ n a m e ) < 6 ) { e c h o ' N a m e n o t l o n g e n o u g h ' ; e x i t ; } } ? > / / F o r m i n p u t e t c .
  14. Object­Oriented Programming Non­linear Series of self­contained objects that can work

    together Modular by design
  15. Benefits Reduced possibility of naming clashes Modularity Reusability

  16. Why and when to OOP?

  17. None
  18. Car Attributes: Colour: Yellow Doors: 2 Luggage rack: true Methods:

    Accelerate Brake Steer
  19. Blog post Attributes: Title: My awesome blog post! Date: 2016­09­06

    Sticky: false Methods: Update Publish Delete
  20. How to OOP?

  21. Language Classes Attributes Methods p r i v a t

    e , p r o t e c t e d , p u b l i c
  22. Class c l a s s C a r {

    / / C l a s s b o d y }
  23. A class is a blueprint for the objects that we

    want to produce
  24. Attributes c l a s s C a r {

    p u b l i c $ c o l o r ; p u b l i c $ d o o r s ; p u b l i c $ l u g g a g e _ r a c k ; }
  25. Methods c l a s s C a r {

    f u n c t i o n a c c e l e r a t e ( ) { e c h o ' T y r e s q u e a l ! ' ; } f u n c t i o n b r a k e ( ) { e c h o ' S c r e e c h ' ; } f u n c t i o n s t e e r ( $ d e g r e e ) { e c h o " S t e e r b y $ d e g r e e d e g r e e s " ; } }
  26. Car Factory c l a s s C a r

    { p u b l i c $ c o l o r = ' w h i t e ' ; p u b l i c $ d o o r s = 2 ; p u b l i c $ l u g g a g e _ r a c k = f a l s e ; f u n c t i o n a c c e l e r a t e ( ) { e c h o ' T y r e s q u e a l ! ' ; } f u n c t i o n b r a k e ( ) { e c h o ' S c r e e c h ' ; } f u n c t i o n s t e e r ( $ d e g r e e ) { e c h o " S t e e r b y $ d e g r e e d e g r e e s " ; } }
  27. Build a Car $ n e w _ c a

    r = n e w C a r ( ) ;
  28. Build a Yellow Car with Two Doors $ y e

    l l o w _ c a r = n e w C a r ( ) ; $ y e l l o w _ c a r ­ > c o l o r = " y e l l o w " ; $ y e l l o w _ c a r ­ > d o o r s = 4 ;
  29. Could Do Better?

  30. Introducing _ _ c o n s t r u

    c t c l a s s C a r { p u b l i c $ c o l o r ; p u b l i c $ d o o r s ; p u b l i c $ l u g g a g e _ r a c k ; f u n c t i o n _ _ c o n s t r u c t ( $ c o l o r , $ d o o r s , $ l u g g a g e _ r a c k ) { $ t h i s ­ > c o l o r = $ c o l o r ; $ t h i s ­ > d o o r s = $ d o o r s ; $ t h i s ­ > l u g g a g e _ r a c k = $ l u g g a g e _ r a c k ; } / / M e t h o d s g o h e r e }
  31. Build a Purple Car with Four Doors and a Luggage

    Rack $ p u r p l e _ c a r = n e w C a r ( ' p u r p l e ' , 4 , t r u e ) ;
  32. Documentation Say hello to phpDocumentor

  33. Documenting Classes / * * * D e f i

    n e s c a r f a c t o r y . * T h e c a r c l a s s p r o d u c e s c a r s * * @ p a c k a g e c a r * @ a u t h o r J a c k L e n o x * @ c o p y r i g h t 2 0 1 6 A u t o m a t t i c I n c . * / c l a s s C a r { / / C l a s s b o d y }
  34. Documenting Properties c l a s s C a r

    { / * * * T h e c a r c o l o r . * U s e d b y t h e m a n u f a c t u r i n g p r o c e s s * @ v a r s t r i n g * / p u b l i c $ c o l o r ; / * * * T h e n u m b e r o f d o o r s . * A l s o u s e d b y t h e m a n u f a c t u r i n g p r o c e s s * @ v a r i n t e g e r * / p u b l i c $ d o o r s ; / * * * W h e t h e r o r n o t t h e c a r h a s a l u g g a g e r u c k . * Y o u ' v e g u e s s e d i t , a l s o f o r m a n u f a c t u r i n g * @ v a r b o o l * / p u b l i c $ l u g g a g e _ r a c k ; / / . . .
  35. Documenting Methods / / . . . / * *

    * A c c e l e r a t e t o m o v e t h e c a r f o r w a r d s . * A c c e l e r a t i o n i s c u r r e n t l y n o t v a r i a b l e s o t h i s * m e t h o d w i l l r e t u r n a s q u e a l f r o m t h e t y r e s a s * t h e w h e e l s s p i n . * @ r e t u r n s t r i n g * / f u n c t i o n a c c e l e r a t e ( ) { r e t u r n ' T y r e s q u e a l ! ' ; } / / . . .
  36. Unit Testing and Test­Driven Development Say hello to PHPUnit

  37. A Basic Test u s e P H P U

    n i t \ F r a m e w o r k \ T e s t C a s e ; r e q u i r e _ o n c e ( ' c l a s s ­ c a r . p h p ' ) ; c l a s s C a r T e s t e x t e n d s T e s t C a s e { p r i v a t e $ c a r ; p u b l i c f u n c t i o n s e t U p ( ) { $ t h i s ­ > c a r = n e w C a r ( ' y e l l o w ' , 4 , t r u e ) ; } p u b l i c f u n c t i o n t e a r D o w n ( ) { } p u b l i c f u n c t i o n t e s t G e t C a r ( ) { $ t h i s ­ > a s s e r t E q u a l s ( $ t h i s ­ > c a r ­ > c o l o r , ' y e l l o w ' ) ; $ t h i s ­ > a s s e r t E q u a l s ( $ t h i s ­ > c a r ­ > d o o r s , 4 ) ; $ t h i s ­ > a s s e r t T r u e ( $ t h i s ­ > c a r ­ > l u g g a g e _ r a c k ) ; } }
  38. Run the Test $ p h p u n i

    t ­ ­ v e r b o s e t e s t s P H P U n i t 5 . 5 . 4 b y S e b a s t i a n B e r g m a n n a n d c o n t r i b u t o r s . R u n t i m e : P H P 7 . 0 . 8 ­ 0 u b u n t u 0 . 1 6 . 0 4 . 2 . 1 / 1 ( 1 0 0 % ) T i m e : 1 0 5 m s , M e m o r y : 8 . 0 0 M B O K ( 1 t e s t , 3 a s s e r t i o n s )
  39. Design Patterns

  40. Singletons and Inheritance

  41. OOP and WP in Practice Widgets API The Walker Class

    WP_Rewrite
  42. Widgets API c l a s s M y _

    W i d g e t e x t e n d s W P _ W i d g e t { / * * * S e t s u p t h e w i d g e t s n a m e e t c * / p u b l i c f u n c t i o n _ _ c o n s t r u c t ( ) { $ w i d g e t _ o p s = a r r a y ( ' c l a s s n a m e ' = > ' m y _ w i d g e t ' , ' d e s c r i p t i o n ' = > ' M y W i d g e t i s a w e s o m e ' , ) ; p a r e n t : : _ _ c o n s t r u c t ( ' m y _ w i d g e t ' , ' M y W i d g e t ' , $ w i d g e t _ o p s ) ; } / / / C o n t i n u e s . . .
  43. / * * * O u t p u t

    s t h e c o n t e n t o f t h e w i d g e t * * @ p a r a m a r r a y $ a r g s * @ p a r a m a r r a y $ i n s t a n c e * / p u b l i c f u n c t i o n w i d g e t ( $ a r g s , $ i n s t a n c e ) { / / o u t p u t s t h e c o n t e n t o f t h e w i d g e t } / * * * O u t p u t s t h e o p t i o n s f o r m o n a d m i n * * @ p a r a m a r r a y $ i n s t a n c e T h e w i d g e t o p t i o n s * / p u b l i c f u n c t i o n f o r m ( $ i n s t a n c e ) { / / o u t p u t s t h e o p t i o n s f o r m o n a d m i n }
  44. / * * * P r o c e s

    s i n g w i d g e t o p t i o n s o n s a v e * * @ p a r a m a r r a y $ n e w _ i n s t a n c e T h e n e w * o p t i o n s * @ p a r a m a r r a y $ o l d _ i n s t a n c e T h e p r e v i o u s * o p t i o n s * / p u b l i c f u n c t i o n u p d a t e ( $ n e w _ i n s t a n c e , $ o l d _ i n s t a n c e ) { / / p r o c e s s e s w i d g e t o p t i o n s t o b e s a v e d } }
  45. The Walker Class ( code.tutsplus.com/tutorials/understanding­ the­walker­class­­wp­25401 )

  46. Conclusion

  47. WPPB ( wppb.me ) Tom McFarlin's Tuts+ Series ( tommcfarlin.com/object­oriented­

    programming­in­wordpress/ )
  48. When not to OOP?

  49. Trouser­Oriented Clothing, Noun­ Oriented Language etc.

  50. Functional Programming? Execution in the Kingdom of Nouns ( steve­yegge.blogspot.sg/2006/03/execution­in­

    kingdom­of­nouns.html )
  51. OOP is not necessarily the "right way"

  52. But it's much better than this!

  53. Thank you github.com/jacklenox/introduction­to­oop automattic.com/work­with­us/