Proxy OOP Pattern in PHP


Published on

An introduction of the Proxy Pattern for PHP Developers.

Covers topics such as Lazy loading, Weak References, Smart References, Protection and Remote proxies.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Proxy OOP Pattern in PHP

  2. 2. MARCO PIVETTADoctrine core teamZf2 contributorWasting time on: OcraDiCompiler OcraServiceManager OcraCachedViewResolver Doctrine ZF2 Modules ZeffMu AssetManager KJSencha Follow @Ocramius Follow @Ocramius
  3. 3. WHAT IS A PROXY?A proxy is generally an object whoseinterface is a layer between us and a different object with the same interface.
  4. 4. Simplified, in PHP Even if were not implementing an interface, the interface ofC s o e P o yis the same as the utmrrx one of C s o e utmr
  5. 5. WHERE TO USE PROXIES?As of the LSP , a proxy can be used inany place where the "proxied" object can be used.
  6. 6. WHEN TO USE A PROXY?Lazy loadingRemote objectsSmart reference/Weak referenceProtectionAOPNull object fallback
  7. 7. LAZY LOADING We may want to use lazy loading toavoid IO operations or heavy loading operation until really needed
  8. 8. LAZY LOADING PATTERNS 1. Virtual Object 2. Value Holder 3. Ghost Object
  9. 9. VIRTUAL OBJECTAn object whose properties are all setto n l , and where each access to the ul properties is tracked. Access triggers lazy loading on the property.
  10. 10. VALUE HOLDER An object that can hold an instance ofthe original proxied object, and loads it only when needed.
  11. 11. GHOST OBJECT An object whose properties are the same of the proxied object, but n l . ulAccessing any method causes loading of the properties. Doctrine Proxies are generated this way.
  12. 12. VALUE HOLDER EXAMPLE (1/3)casIae{ls mg pbi fnto _cntut$ah { ulc ucin _osrc(pt) $hs>mg =iaeraermpg$ ti-iae mgcetfoje(pah;t) } pbi fnto gtie){ ulc ucin eSz( rtr aryiaex$hs>mg) i eun ra(mgs(ti-iae,mgs(ti-iae) aey$hs>mg); }}
  13. 13. VALUE HOLDER EXAMPLE (2/3)casIaerx etnsIae{ls mgPoy xed mg poetd$mg; rtce iae pbi fnto _cntut$ah { ulc ucin _osrc(pt) $hs>ah=$ah ti-pt pt; } piaefnto ii( { rvt ucin nt) i (!$hs>mg){ f ti-iae $hs>mg =nwIae$hs> ti-iae e mg(ti-pt)ah; } } pbi fnto gtie){ ulc ucin eSz( $hs>nt) ti-ii(; rtr $hs>mg-gtie) eun ti-iae>eSz(; }}
  14. 14. VALUE HOLDER EXAMPLE (3/3)$m1=nwIaerx(/aht/mg1jg) ig e mgPoypt/oiae.p;vrdm(eoygtuae);/ ~0K a_upmmr_e_sg() / 20b$m2=nwIaerx(/aht/mg2jg) ig e mgPoypt/oiae.p;vrdm(eoygtuae);/ ~0K a_upmmr_e_sg() / 20b$m3=nwIaerx(/aht/mg3jg) ig e mgPoypt/oiae.p;vrdm(eoygtuae);/ ~0K a_upmmr_e_sg() / 20b$ie =$m1>eSz(;sz1 ig-gtie)vrdm(eoygtuae);/ ~Ma_upmmr_e_sg() / 4b$ie =$m2>eSz(;sz2 ig-gtie)vrdm(eoygtuae);/ ~Ma_upmmr_e_sg() / 8b
  15. 15. LAZY LOADING PROS/CONSAdvantages Low memory impact Low overhead Easy to implement Useful to determine object "dirty" statusDis-advantages Not optimal for data that is always loaded Lazy loading means lazy failing
  16. 16. REMOTE OBJECTIt basically is a specific form of lazy loading
  17. 17. REMOTE OBJECT EXAMPLE (1/3)casTet{ls we poetd$aa rtce dt; pbi fnto _cntutary$aa ulc ucin _osrc(ra dt){ $hs>aa=$aa ti-dt dt; } pbi fnto gtet){ ulc ucin eTx( rtr $hs>aaet] eun ti-dt[tx; }}
  18. 18. REMOTE OBJECT EXAMPLE (2/3)casTetrx etnsTet{ls wePoy xed we poetd$p;poetd$we;poe rtce ai rtce tet rtctd$de i; pbi fnto _cntutTitrp $ ulc ucin _osrc(wteAiai $d { p, i) $hs>p =$p;$hs>d=$d ti-ai ai ti-i i; } piaefnto ii( { rvt ucin nt) i (!$hs>we){ f ti-tet $hs>we =nwTet$hs> ti-tet e we(ti-ai>e(ti-i); p-gt$hs>d) } } pbi fnto gtet){ ulc ucin eTx( $hs>nt) ti-ii(; rtr $hs>we-gtet) eun ti-tet>eTx(; }}
  19. 19. REMOTE OBJECT EXAMPLE (3/3)$we =nwTetaryet = rxe itet e we(ra(tx > PoisnPP) H!);vrdm(tet>eTx() / rxe i Pa_up$we-gtet); / Pois nH!P$p =nwTitrp(*yda*) / z,bai e wteAi/ ad /; / f uz,ecz t$eoewe =nwTetrx(ai 20478rmtTet e wePoy$p, 863098850;6366)vrdm(rmtTet>eTx() / wea_up$eoewe-gtet); / Tettx!et$eoewe =nwTetrx(ai 20478rmtTet e wePoy$p, 863098851;6366)vrdm(rmtTet>eTx() / ntea_up$eoewe-gtet); / Aohrtx! et
  20. 20. REMOTE OBJECT PROS/CONSAdvantages Abstraction of a remote object You can re-define the proxied objects API locallyDis-advantages To use such a proxy, you almost always need a configured remote client Remote objects fail very easily
  21. 21. SMART REFERENCE Smart reference can be used to:swap the proxied object at runtimereference singletons or use internalstatic registriesOptimize memory usage
  22. 22. SMART REFERENCE WITH WEAKREFUsing Weakref to save memory on long-running processescasIaerx etnsIae{ls mgPoy xed mg / [.](e peiu eape / .. se rvos xml) piaefnto ii( { rvt ucin nt) i (!$hs>mgRf| !$hs>m f ti-iaee | ti-iaee-vld) {gRf>ai() $hs>mgRf=nwWaRfnw ti-iaee e eke(eIae$hs>ah) mg(ti-pt); } rtr $hs>mgRf>e(; eun ti-iaee-gt) } pbi fnto gtie){ ulc ucin eSz( rtr $hs>nt)>eSz(; eun ti-ii(-gtie) }}
  23. 23. SMART REFERENCE WITH A REGISTRYcasIaerx etnsIae{ls mgPoy xed mg / [.](e peiu eape / .. se rvos xml) piaefnto ii( { rvt ucin nt) i (ul==$hs>mg){ f nl = ti-iae $hs>mg =Iaeeity:e ti-iae mgRgsr:gt(ti-pt)$hs>ah; } } pbi fnto gtie){ ulc ucin eSz( $hs>nt) ti-ii(; rtr $hs>mg-gtie) eun ti-iae>eSz(; }}
  24. 24. SMART REFERENCE PROS/CONSMainly memory usage, but it depends on how you setup your "smart" reference
  25. 25. PROTECTION PROXYProtection proxy comes into play whenyou want to transparently limit access to an API through a set of rules (ACL/limits)
  26. 26. PROTECTION PROXY (1/2)casRmtAirx etnsRmtAi{ls eoepPoy xed eoep poetd$on =0 rtce cut ; pbi fnto _cntutRmtAi$ ulc ucin _osrc(eoep ap,$ii){i lmt $hs>p =$p;$hs>ii =$ ti-ai ai ti-lmt lii;mt } piaefnto cut){ rvt ucin on( i (+ti-cut>$hs>ii){ f +$hs>on ti-lmt trwnwRmtAiii(SAP ho e eoepLmtTH!;) } } pbi fnto dSuf){ ulc ucin otf( $hs>on(; ti-cut) rtr $hs>p-dSuf) eun ti-ai>otf(; }}
  27. 27. PROTECTION PROXY (2/2)$p =nwRmtAirx(e RmtAi/ .ai e eoepPoynw eoep(* ..*) 5) /, 0;wie(){ hl 1 $p-dSuf) / RmtAiii ecp ai>otf(; / eoepLmt xetin o!}
  28. 28. PROTECTION PROXY PROS/CONSAdvantages Transparent filtering or limiting of access to an objectDis-advantages Modifies proxied object behavior! More like a decorator!
  29. 29. NULL OBJECT FALLBACK PROXY A null object is an object thatimplements an interface, but produces no side effects. It replaces n l . ul Using null objects allows us to workwith the assumption that an object will always be available, reducing checks against n l by a lot. ul
  30. 30. NULL OBJECT FALLBACK PROXY EXAMPLEcasCsoePoyetnsCsoe { ls utmrrx xed utmr pbi fnto _cntutD $b $d ulc ucin _osrc(b d, i){ $hs>utmr=$b>id$d; ti-csoe d-fn(i) i (!$hs>utmr { f ti-csoe) $hs>utmr=nwNlCsoe ti-csoe e ulutmr)(; } } pbi fnto dSuf){ ulc ucin otf( rtr $hs>utmr>otf(; eun ti-csoe-dSuf) }}
  31. 31. NULL OBJECT FALLBACK PROXY PROSPerformance (with small # ofinstances)Reduced NPaths, therefore code iseasier to test
  32. 32. AOP AND PROXIESProxies basically enable us to have logic between us and any object, making AOP easy even when the language doesnt allow it. We can use code generation to createon-the-fly proxies with our custom AOP logic executed pre- and post- any method of the proxied object.
  33. 33. AOP EXAMPLES/* * *@Oah(t=60 APCcetl30) */pbi fnto dHaytf( { ulc ucin oevSuf) / [.] / ..} Becomes:pbi fnto dHaytf( {ulc ucin oevSuf) i(cce =$hs>ah-gtoevS f$ahd ti-cce>e(dHaytuf,fn_e_rs)){f ucgtag() rtr $ahd eun cce; } $eun=$hs>rgnlbet>oevS rtr ti-oiiaOjc-dHaytf(;uf) $hs>ah-st$eun oevSuf ti-cce>e(rtr, dHaytf,fn_e_rs); ucgtag() rtr $eun eun rtr;}
  35. 35. IMPLEMENTING THE PUBLIC API1. The Proxy class MUST extend the proxied class2. Each of the proxied methods must be rewritten3. Proxies should be serializable4. Proxies should handle public properties
  36. 36. PUBLIC PROPERTIES PROXYINGcasCsoe { ls utmr pbi $ae ulc nm; pbi $unm; ulc srae}casCsoePoyetnsCsoe {ls utmrrx xed utmr pbi fnto _cntutCsoe $u ulc ucin _osrc(utmr cstmr {oe) ust$hs>ae $hs>unm) ne(ti-nm, ti-srae; $hs>utmr=$utmr ti-csoe csoe; } pbi fnto _st$ae $au){ ulc ucin _e(nm, vle $hs>utmr>nm =$au; ti-csoe-$ae vle } pbi fnto _gt$ae { ulc ucin _e(nm) rtr $hs>utmr>nm; eun ti-csoe-$ae } / _ist _ust / _se, _ne}
  38. 38. SOME USEFUL LIBRARIEShttp://flow.typo3.org
  39. 39. CODE GENERATIONSee Doctrines Proxy Generator
  40. 40. QUESTIONS?