Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

OOP

1.
Patterns and OOP in PHP George Schlossnagle <george@omniti.com>

2.
<ul><li>Patterns catalog solutions to categories of problems </li></ul><ul><li>They consist of </li></ul><ul><ul><li>A name </li></ul></ul><ul><ul><li>A description of their problem </li></ul></ul><ul><ul><li>A description of the solution </li></ul></ul><ul><ul><li>An assessment of the pros and cons of the pattern </li></ul></ul>What are Patterns?

4.
<ul><li>Brining </li></ul><ul><li>Problem: Make lean meat jucier </li></ul><ul><li>Solution: Submerge the meat in a salt and flavor infused liquid. </li></ul><ul><li>Discussion: Salt denatures the meat proteins, allowing them to trap liquid between them more efficiently. </li></ul><ul><li>Example: Mix 1C of table salt and 1C of molasses into 1G of water. Bring to a boil to dissolve. Submerge pork roast for 2 days. </li></ul>A tasty design pattern

5.
<ul><li>OOP is paradigm more than a feature set. </li></ul><ul><li>Everyone is a bit different, and they all think they're right </li></ul><ul><li>The classic difference </li></ul><ul><ul><li>click(button) vs. button.click() </li></ul></ul>What is OOP?

6.
<ul><li>Let’s try to define OOP through the values it tries to promote. </li></ul><ul><ul><li>Allow compartmentalized refactoring of code </li></ul></ul><ul><ul><li>Promote code reuse </li></ul></ul><ul><ul><li>Promote extensability </li></ul></ul><ul><li>Is OOP the only solution for this? </li></ul><ul><ul><li>Of course not. </li></ul></ul>Rephrase: What is its motivation?

7.
<ul><li>Encapsulation is about grouping of related properties and operations into classes. </li></ul><ul><li>Classes represent complex data types and the operations that act on them. An object is a particular instance of a class. For example ‘Dog’ may be a class (it’s a type of thing), while Grendel (my dog) is an instance of that class. </li></ul>Encapsulation

8.
<ul><li>Classses as dictionaries are a common idiom, seen in C: typedef struct _entry { time_t date; char *data; char *(*display)(struct _entry *e); } entry; e->display(e); </li></ul><ul><li>You can see this idiom in Perl and Python, both of which prototype class methods to explicitly grab $this (or their equivalent). </li></ul>Are Classes Just Dictionaries?

15.
<ul><li>Multiple inheritance is confusing. If you inherit from ClassA and ClassB, and they both define method foo(), whose should you inherit? </li></ul><ul><li>Interfaces allow you to specify the functionality that your class must implement. </li></ul><ul><li>Type hints allow you to require (runtime checked) that an object passed to a function implements or inherits certain required facilities. </li></ul>Multiple Inheritance

17.
<ul><li>Abstract classes provide you a cross between a ‘real’ class and an interface. They are classes where certain methods are defined, and other methods are only prototyped. </li></ul><ul><li>Abstract classes are useful for providing a base class that should never be instantiated. </li></ul><ul><li>abstract class CalendarEntry { abstract function display(); public function fetchDetails() { /*...*/} public function saveDetails() {/*...*/} } </li></ul>Abstract Classes

18.
<ul><li>One of the notions of OOP is that your package/library should have a public API that users should interact with. What happens behind the scenes is none of their business, as long as this public API is stable. This separation is often referred to as ‘data hiding’ or ‘implementation hiding’. </li></ul><ul><li>Some languages (Perl, Python) rely on a ‘gentleman’s contract’ to enforce this separation, while other languages enforce it as a language feature. </li></ul>Public Relations

19.
<ul><li>PHP implements strict visibility semantics. Data hiding eases refactoring by controlling what other parties can access in your code. </li></ul><ul><ul><li>public anyone can access it </li></ul></ul><ul><ul><li>protected only descendants can access it </li></ul></ul><ul><ul><li>private only you can access it </li></ul></ul><ul><ul><li>final no one can re-declare it. </li></ul></ul><ul><ul><li>Why have these in PHP? Because sometimes self-discipline isn’t enough. </li></ul></ul>Data Hiding

21.
<ul><li>In an OOP paradigm this would look like: foreach($entries as $entry) { $entry->display(); } </li></ul><ul><li>The key point is we don't have to modify this loop to add new types. When we add a new type, that type gets a display() method so it know how to display itself, and we’re done. </li></ul><ul><li>(p.s. this is a good case for the aggregate pattern, shown later) </li></ul>Simplicity Through Polymorphism

23.
<ul><li>Problem: You only want one instance of an object to ever exist at one time </li></ul><ul><li>Solutions: </li></ul><ul><ul><li>PHP4: Use a factory method with static cache </li></ul></ul><ul><ul><li>PHP4: Use a global cache and runtime instance mutability </li></ul></ul><ul><ul><li>PHP5: Use static class attributes </li></ul></ul>Singleton Pattern

26.
<ul><li>Problem: You have collections of items that you operate on frequently with lots of repeated code. </li></ul><ul><li>Remember our calendars: foreach($entries as $entry) { $entry->display(); } </li></ul><ul><li>Solution: Create a container that implements the same interface, and perfoms the iteration for you. </li></ul>Aggregator Pattern

28.
<ul><li>Problem: You need to be able to iterate through an aggregation. </li></ul><ul><li>Solution: Implement all the operations necessary to logically iterate over an object. In PHP this is now a built in facility so that object implementing the Iterator interface can be used in the language array constructs like foreach(). </li></ul>Iterator Pattern

30.
<ul><li>Why not just use: foreach($aggregate as $item) { /*...*/ } </li></ul><ul><li>Aren't we making life more difficult than need be? </li></ul><ul><li>No! For simple aggregations the above works fine, but not everything is an array. What about: </li></ul><ul><ul><li>Buffered result sets </li></ul></ul><ul><ul><li>Directories </li></ul></ul><ul><ul><li>Anything not already an array </li></ul></ul>Aren’t Iterators Pointless in PHP?

31.
<ul><li>Problem: You need to provide access to an object, but it has an interface you don’t know at compile time. </li></ul><ul><li>Solution: Use accessor/method overloading to dynamically dispatch methods to the object. </li></ul><ul><li>Discussion: This is very typical of RPC-type facilities like SOAP where you can interface with the service by reading in a definitions file of some sort at runtime. </li></ul>Proxy Pattern

33.
<ul><li>Problem: You want an object to automatically notify dependents when it is updated. </li></ul><ul><li>Solution: Allow 'observer' to register themselves with the observable object. </li></ul><ul><li>Discussion: An object may not apriori know who might be interested in it. The Observer pattern allows objects to register their interest and supply a notification method. </li></ul>Observer Pattern

35.
THANKS! <ul><li>Slides for this talk will be available shortly at http://www.omniti.com/~george/talks/ </li></ul><ul><li>A longer version with 3 hours of info and a greater focus on patterns and advanced features will be presented by Marcus Boerger and myself at OSCON in July. Come see us there! </li></ul><ul><li>Shameless book plug: Buy my book, you’ll like it. I promise. </li></ul>