This document discusses the proxy pattern in PHP. It defines a proxy as an object whose interface sits between the client and a different object with the same interface. Proxies are commonly used for lazy loading, handling remote objects, implementing smart references, adding protection layers, and providing null object fallbacks. The document provides examples of implementing proxies for value holders, remote objects, smart references using weak references and registries, protection proxies, and null object fallback proxies. It also discusses how to implement the public API of a proxy class in PHP by extending the proxied class and rewriting its methods while handling public properties and serialization. Useful proxy libraries and code generation techniques are mentioned.
10. VIRTUAL OBJECT
An object whose properties are all set
to n l , and where each access to the
ul
properties is tracked.
Access triggers lazy loading on the
property.
11. VALUE HOLDER
An object that can hold an instance of
the original proxied object, and loads it
only when needed.
12. GHOST OBJECT
An object whose properties are the
same of the proxied object, but n l .
ul
Accessing any method causes loading of
the properties.
Doctrine Proxies are generated this
way.
13. VALUE HOLDER EXAMPLE
(1/3)
casIae{
ls mg
pbi fnto _cntut$ah {
ulc ucin _osrc(pt)
$hs>mg =iaeraermpg$
ti-iae mgcetfoje(p
ah;
t)
}
pbi fnto gtie){
ulc ucin eSz(
rtr aryiaex$hs>mg) i
eun ra(mgs(ti-iae,
mgs(ti-iae)
aey$hs>mg);
}
}
14. 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(;
}
}
16. LAZY LOADING PROS/CONS
Advantages
Low memory impact
Low overhead
Easy to implement
Useful to determine object "dirty"
status
Dis-advantages
Not optimal for data that is always
loaded
Lazy loading means lazy failing
18. 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>aa'et]
eun ti-dt[tx';
}
}
19. REMOTE OBJECT EXAMPLE
(2/3)
casTetrx etnsTet{
ls wePoy xed we
poetd$p;poetd$we;poe
rtce ai rtce tet rtc
td$d
e i;
pbi fnto _cntutTitrp $
ulc ucin _osrc(wteAi
ai $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(;
}
}
20. REMOTE OBJECT EXAMPLE
(3/3)
$we =nwTetary'et = 'rxe i
tet e we(ra(tx' > Pois
nPP')
H!);
vrdm(tet>eTx() / 'rxe i P
a_up$we-gtet); / Pois n
H!
P'
$p =nwTitrp(*yda*) / z,b
ai e wteAi/ ad /; / f u
z,ec
z t
$eoewe =nwTetrx(ai 20478
rmtTet e wePoy$p, 8630
98850;
6366)
vrdm(rmtTet>eTx() / 'we
a_up$eoewe-gtet); / Tet
tx!
et'
$eoewe =nwTetrx(ai 20478
rmtTet e wePoy$p, 8630
98851;
6366)
vrdm(rmtTet>eTx() / 'nte
a_up$eoewe-gtet); / Aoh
rtx!
et'
21. REMOTE OBJECT PROS/CONS
Advantages
Abstraction of a remote object
You can re-define the proxied
object's API locally
Dis-advantages
To use such a proxy, you almost
always need a configured remote
client
Remote objects fail very easily
22. SMART REFERENCE
Smart reference can be used to:
swap the proxied object at runtime
reference singletons or use internal
static registries
Optimize memory usage
23. SMART REFERENCE WITH
WEAKREF
Using Weakref to save memory on
long-running processes
casIaerx 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-i
aee-vld) {
gRf>ai()
$hs>mgRf=nwWaRfnw
ti-iaee e eke(e
Iae$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)
}
}
24. SMART REFERENCE WITH A
REGISTRY
casIaerx 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(;
}
}
25. SMART REFERENCE
PROS/CONS
Mainly memory usage, but it depends
on how you setup your "smart"
reference
26. PROTECTION PROXY
Protection proxy comes into play when
you want to transparently limit access
to an API through a set of rules
(ACL/limits)
27. PROTECTION PROXY (1/2)
casRmtAirx etnsRmtAi{
ls eoepPoy xed eoep
poetd$on =0
rtce cut ;
pbi fnto _cntutRmtAi$
ulc ucin _osrc(eoep a
p,$ii){
i lmt
$hs>p =$p;$hs>ii =$
ti-ai ai ti-lmt l
ii;
mt
}
piaefnto cut){
rvt ucin on(
i (+ti-cut>$hs>ii){
f +$hs>on ti-lmt
trwnwRmtAiii(SAP
ho e eoepLmt'TH!
';
)
}
}
pbi fnto dSuf){
ulc ucin otf(
$hs>on(;
ti-cut)
rtr $hs>p-dSuf)
eun ti-ai>otf(;
}
}
29. PROTECTION PROXY
PROS/CONS
Advantages
Transparent filtering or limiting of
access to an object
Dis-advantages
Modifies proxied object behavior!
More like a decorator!
30. NULL OBJECT FALLBACK
PROXY
A null object is an object that
implements an interface, but produces
no side effects. It replaces n l .
ul
Using null objects allows us to work
with the assumption that an object will
always be available, reducing checks
against n l by a lot.
ul
31. NULL OBJECT FALLBACK
PROXY EXAMPLE
casCsoePoyetnsCsoe {
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 ulutm
r)
(;
}
}
pbi fnto dSuf){
ulc ucin otf(
rtr $hs>utmr>otf(;
eun ti-csoe-dSuf)
}
}
32. NULL OBJECT FALLBACK
PROXY PROS
Performance (with small # of
instances)
Reduced NPaths, therefore code is
easier to test
33. AOP AND PROXIES
Proxies basically enable us to have logic
between us and any object, making
AOP easy even when the language
doesn't allow it.
We can use code generation to create
on-the-fly proxies with our custom AOP
logic executed pre- and post- any
method of the proxied object.
36. IMPLEMENTING THE PUBLIC
API
1. The Proxy class MUST extend the
proxied class
2. Each of the proxied methods must be
rewritten
3. Proxies should be serializable
4. Proxies should handle public
properties