Proxy OOP Pattern in PHP
Upcoming SlideShare
Loading in...5
×
 

Proxy OOP Pattern in PHP

on

  • 4,592 views

An introduction of the Proxy Pattern for PHP Developers.

An introduction of the Proxy Pattern for PHP Developers.

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

Statistics

Views

Total Views
4,592
Views on SlideShare
4,592
Embed Views
0

Actions

Likes
1
Downloads
19
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Proxy OOP Pattern in PHP Proxy OOP Pattern in PHP Document Transcript

  • PROXY PATTERN IN PHP
  • MARCO PIVETTADoctrine core teamZf2 contributorWasting time on: OcraDiCompiler OcraServiceManager OcraCachedViewResolver Doctrine ZF2 Modules ZeffMu AssetManager KJSencha Follow @Ocramius Follow @Ocramius
  • WHAT IS A PROXY?A proxy is generally an object whoseinterface is a layer between us and a different object with the same interface.
  • 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
  • WHERE TO USE PROXIES?As of the LSP , a proxy can be used inany place where the "proxied" object can be used.
  • WHEN TO USE A PROXY?Lazy loadingRemote objectsSmart reference/Weak referenceProtectionAOPNull object fallback
  • LAZY LOADING We may want to use lazy loading toavoid IO operations or heavy loading operation until really needed
  • LAZY LOADING PATTERNS 1. Virtual Object 2. Value Holder 3. Ghost Object
  • 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.
  • VALUE HOLDER An object that can hold an instance ofthe original proxied object, and loads it only when needed.
  • 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.
  • 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); }}
  • 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(; }}
  • 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
  • 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
  • REMOTE OBJECTIt basically is a specific form of lazy loading
  • 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; }}
  • 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(; }}
  • 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
  • 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
  • SMART REFERENCE Smart reference can be used to:swap the proxied object at runtimereference singletons or use internalstatic registriesOptimize memory usage
  • 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) }}
  • 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(; }}
  • SMART REFERENCE PROS/CONSMainly memory usage, but it depends on how you setup your "smart" reference
  • PROTECTION PROXYProtection proxy comes into play whenyou want to transparently limit access to an API through a set of rules (ACL/limits)
  • 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(; }}
  • 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!}
  • PROTECTION PROXY PROS/CONSAdvantages Transparent filtering or limiting of access to an objectDis-advantages Modifies proxied object behavior! More like a decorator!
  • 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
  • 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) }}
  • NULL OBJECT FALLBACK PROXY PROSPerformance (with small # ofinstances)Reduced NPaths, therefore code iseasier to test
  • 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.
  • 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;}
  • IMPLEMENTATION DETAILS IN PHP
  • 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
  • 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}
  • A COMPLETE GHOST OBJECT IMPLEMENTATIONhttps://gist.github.com/4038004
  • SOME USEFUL LIBRARIEShttp://flow.typo3.orghttps://github.com/schmittjoh/cg-libraryhttps://github.com/doctrine/commonhttps://github.com/lisachenko/go-aop-php
  • CODE GENERATIONSee Doctrines Proxy Generator
  • QUESTIONS?