<!DOCTYPE html>
<html class="no-js" id="top">
<head>
    <title>ProxyManager - Tuning the ProxyManager for production</title>

    <meta name="description" content="A proxyManager write in php" />
    <meta name="keywords" content="ProxyManager, proxy, manager, ocramius, Marco Pivetta, php, production" />
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
    <link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:200,300,400,600' rel='stylesheet' type='text/css'>
    <link href="css/styles.css" rel="stylesheet" />
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/styles/default.min.css">
    <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/highlight.min.js"></script>
    <script>hljs.initHighlightingOnLoad();</script>
    <link rel="shortcut icon" href="favicon.ico">
</head>
<body>

<header class="site-header">
<div class="container">
<h1><a href="index.html"><img alt="ProxyManager" src="img/block.png" /></a></h1>

<nav class="main-nav" role="navigation">
<ul>
    <li><a href="https://github.com/Ocramius/ProxyManager" target="_blank">Github</a>
    <div class="bcms-clearfix"></div>
</li>
</ul>
</nav>
</div>
</header>
<main role="main">
<section class="component-content">

<div class="component-demo" id="live-demo">
    <div class="container">
            <div class="main-wrapper" style="text-align: right">
                <iframe src="http://ghbtns.com/github-btn.html?user=ocramius&amp;repo=ProxyManager&amp;type=fork&amp;count=true&amp;size=large"
  allowtransparency="true" frameborder="0" scrolling="0" width="310" height="40"></iframe>

                <iframe src="http://ghbtns.com/github-btn.html?user=ocramius&amp;repo=ProxyManager&amp;type=watch&amp;count=true&amp;size=large"
  allowtransparency="true" frameborder="0" scrolling="0" width="200" height="40"></iframe>

            </div>
        <div class="bcms-clearfix bcms-clearfix"></div>
    </div>
</div>
<div class="component-info">
<div class="container">
<aside class="sidebar">
    <nav class="spy-nav">
        <ul>
            <li><a href="index.html">Intro</a></li>
            <li><a href="virtual-proxy.html">Virtual Proxy</a></li>
            <li><a href="null-object.html">Null Objects</a></li>
            <li><a href="ghost-object.html">Ghost Objects</a></li>
            <li><a href="remote-object.html">Remote Object</a></li>
            <li><a href="contributing.html">Contributing</a></li>
            <li><a href="credits.html">Credits</a></li>
            <li><a href="copyright.html">Copyright</a></li>
        </ul>
    </nav>
<div class="bcms-clearfix bcms-clearfix"></div>
<a class="btn btn-action btn-full download-component"
    href="download.html">Download</a>
    <div class="bcms-clearfix"></div>
</aside>

<div class="content">
    <div class="bcms-clearfix"></div>
    <h3 class="section-title">Access Interceptor Scope Localizer Proxy</h3>

    <p>An access interceptor scope localizer is a smart reference proxy that allows you to dynamically define logic to be executed before or after any of the proxied object's methods' logic.</p>
    
    <p>It works exactly like the <a href="access-interceptor-value-holder-proxy.html">access interceptor value holder</a>, with some minor differences in behavior.</p>

    <p>The working concept of an access interceptor scope localizer is to localize scope of a proxied object:</p>
   
    <pre>
        <code class="php">
class Example
{
    protected $foo;
    protected $bar;
    protected $baz;

    public function doFoo()
    {
        // ...
    }
}

class ExampleProxy extends Example
{
    public function __construct(Example $example)
    {
        $this->foo = &amp; $example->foo;
        $this->bar = &amp; $example->bar;
        $this->baz = &amp; $example->baz;
    }

    public function doFoo()
    {
        return parent::doFoo();
    }
}
        </code>
    </pre>

    <p>This allows to create a mirror copy of the real instance, where any change in the proxy or in the real instance is reflected in both objects.</p>

    <p>The main advantage of this approach is that the proxy is now safe against fluent interfaces, which would break an <a href="access-interceptor-value-holder-proxy.html">access interceptor value holder</a> instead.</p>
<hr />    
    <h3>Differences with <a href="access-interceptor-value-holder-proxy.html">access interceptor value holder</a>:</h3>
    
    <ul>
        <li>It does <strong>NOT</strong> implement the <code>ProxyManager\Proxy\ValueHolderInterface</code>, since the proxy itself does not keep a reference to the original object being proxied</li>
        <li>In all interceptor methods (see <a href="access-interceptor-value-holder-proxy.html">access interceptor value holder</a>), the $instance passed in is the proxy itself. There is no way to gather a reference to the original object right now, and that's mainly to protect from misuse.</li>
    </ul>
<hr />

    <h3>Known limitations</h3>

    <ul>
        <li>It is <strong>NOT</strong> possible to intercept access to public properties</li>
        <li>It is <strong>NOT</strong> possible to proxy interfaces, since this proxy relies on <code>parent::method()</code> calls. Interfaces obviously don't provide a parent method implementation.</li>
        <li>calling unset on a property of an access interceptor scope localizer (or the real instance) will cause the two objects to be un-synchronized, with possible unexpected behaviour.</li>
        <li>serializing or un-serializing an access interceptor scope localizer (or the real instance) will not cause the real instance (or the proxy) to be serialized or un-serialized</li>
        <li>if a proxied object contains private properties, then an exception will be thrown if you use PHP <code>&lt; 5.4.0</code>.</li>
    </ul>
<hr />
    <h3>Example</h3>

    <p>Here's an example of how you can create and use an access interceptor scope localizer :</p>

    <pre>
        <code class="php">
&lt;?php

use ProxyManager\Factory\AccessInterceptorScopeLocalizerFactory as Factory;

require_once __DIR__ . '/vendor/autoload.php';

class Foo
{
    public function doFoo()
    {
        echo "Foo!\n";
    }
}

$factory = new Factory();

$proxy = $factory->createProxy(
    new Foo(),
    array('doFoo' => function () { echo "PreFoo!\n"; }),
    array('doFoo' => function () { echo "PostFoo!\n"; })
);

$proxy->doFoo();
        </code>
    </pre>

    <p>This send something like following to your output:</p>

    <pre>
        <code class="php">
PreFoo!
Foo!
PostFoo!
        </code>
    </pre>

    <p>This is pretty much the same logic that you can find in <a href="access-interceptor-value-holder-proxy.html">access interceptor value holder</a>.</p>
    
</main>

    <footer class="site-footer" role="contentinfo">
        <div class="container">
            <div class="footer-logos">
                <ul>
                    <li><a href="index.html">Intro</a> | </li>
                    <li><a href="virtual-proxy.html">Virtual Proxy</a> | </li>
                    <li><a href="null-object.html">Null Objects</a> | </li>
                    <li><a href="ghost-object.html">Ghost Objects</a> | </li>
                    <li><a href="remote-object.html">Remote Object</a> | </li>
                    <li><a href="contributing.html">Contributing</a> | </li>
                    <li><a href="credits.html">Credits</a> | </li>
                    <li><a href="copyright.html">Copyright</a></li>
                </ul>
            </div>
        </div>

        <div class="bcms-clearfix"></div>
    </footer>
    <div class="bcms-clearfix"></div>
    </body>
</html>