In defense of object-oriented programming - How and why you should use object oriented programming for your next project. This talk is for PHP programmers who are just learning about object oriented code, who cling to old excuses ("object oriented code is slower"), or who are otherwise unconvinced of its usefulness. Concrete real-world examples of common scenarios and challenges that programmers face will be presented, and how taking an object oriented approach is better than a procedural one in most cases. Copious code examples in both object oriented and procedural approaches will be provided throughout, and the differences and benefits of the object oriented approach will be explained.
2. What? 2 Not an apology Greek root - apologia (απολογία) - “speaking in defense” To defend the use of, and provide rational reasoning for
3. 3 Who is it for? For People Who: Are “on the fence” about OOP vs procedural Are unconvinced of the usefulness of OOP Want to learn WHY they should learn about OOP
11. Making things that are not the same look the same Relies on a defined interface Inheritance is probably the most used method The ability of type A to be used like type B Think: Interchangeable types or components 11 Polymorphism
16. Extend from a parent class “is-a” relationship Creates hierarchal relationship Get functionality for free Global changes are easier Inherits all functions and properties from parent Think: A is a B, with a few differences 16 Inheritance
21. Hide specific implementation details Reveal only methods and properties required to interact with the object Limits interdependencies between components Think: Separation of responsibilities 21 Encapsulation
30. Uses __get() “magic” method in PHP5 object model Also uses SPL interfaces to fire query on: count() foreach() Used as a hook to retrieve related rows on call Caches results so only 1 query is fired Can eliminate N+1 query problem by mapping 27 Lazy-Loading: OOP Only
31. Classes are actually custom types Can type-hint for classes or interfaces PHP Standard types: string, int, float, boolean, array, object, null resource 28 Custom Type Creation & Type-Hinting
33. Easily group related properties/data Avoid using globals or passing/returning arrays Suppress errors ala ‘undefined index’ More? Convenience (a.k.a. Laziness) 30
34. Request object: Why not $_POST? Data comes from multiple sources POST/GET, XML, JSON, etc. Other functions isAjax(), isPost(), etc. Sanitizing user input Session object: Why not $_SESSION? More options for saving/storing Database, separate server, memcached, etc. 31 Request / Session Objects
47. So far it’s OK It works We finished and worked quickly 38 Status Check
48. 39 Client Message I talked to my next door neighbor’s cousin’s brother’s niece yesterday, and he says all the serious online stores have regular sales. That’s something I can do too, right? - Bob
51. We also have to add this code to the admin backend for customer invoices. And to the email reciepts Sin of code duplication Code smell 42 Thoughts
52. 43 Client Message Hey,I was at the grocery store yesterday and my daughter got 2 candy bars for $1, when they were originally $0.75 each. I know that if I am able to do this, I’ll get a lot of sales and it will make me rich. I need to be able to do this. - Bob
55. We also have to add this code to the admin backend and other places again. Sin of code duplication We could use procedural functions for this Where would we put them? What responsibilities do they have? 46 Thoughts
60. Clearly, as the project grows, it will become a maintenance nightmare if we continue on the current path. We don’t want our code to be the running joke of the PHP community. 51 We need something better
64. Create a Cart class to store items Encapsulate the pricing logic in an Item class Single place to change the code Item is responsible for knowing it’s price (?) What does this imply? 55 Thoughts
66. Still storing cart in session, but now we can change it later when we need to scale Cart gets item price so it can check quantities Cart is responsible for knowing other items in cart Better separation of responsibility It’s not the job of the display logic to calculate the item’s price or apply discounts What about changing to FedEx? 57 Thoughts
68. Package is responsible for knowing it’s own dimensions and weight Quote is responsible for fetching a live rate quote from a carrier API service Always think in terms of responsibility What code is responsible for what functions? Where does it go in my app? Is this code doing too much? 59 Thoughts
77. You must set goals that will help direct you to your desired outcome Goals narrow attention and direct efforts to goal-relevant activities, and away from perceived undesirable and goal-irrelevant actions Re-use as a goal does not help you write good OO code. Re-use is a by-product of good OO. 68 Point: Set Good Goals
78. 69 Myth #2: Objects should always be modeled after real-world objects when possible
79. 70 Truth: Objects should be modeled and built based on what you need to complete your task