[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/docs/ -> access-interceptor-scope-localizer.md (source)

   1  # Access Interceptor Scope Localizer Proxy
   2  
   3  An access interceptor scope localizer is a smart reference proxy that allows you to dynamically
   4  define logic to be executed before or after any of the proxied object's methods' logic.
   5  
   6  It works exactly like the [access interceptor value holder](access-interceptor-value-holder.md),
   7  with some minor differences in behavior.
   8  
   9  The working concept of an access interceptor scope localizer is to localize scope of a proxied object:
  10  
  11  ```php
  12  class Example
  13  {
  14      protected $foo;
  15      protected $bar;
  16      protected $baz;
  17  
  18      public function doFoo()
  19      {
  20          // ...
  21      }
  22  }
  23  
  24  class ExampleProxy extends Example
  25  {
  26      public function __construct(Example $example)
  27      {
  28          $this->foo = & $example->foo;
  29          $this->bar = & $example->bar;
  30          $this->baz = & $example->baz;
  31      }
  32  
  33      public function doFoo()
  34      {
  35          return parent::doFoo();
  36      }
  37  }
  38  ```
  39  
  40  This allows to create a mirror copy of the real instance, where any change in the proxy or in the real
  41  instance is reflected in both objects.
  42  
  43  The main advantage of this approach is that the proxy is now safe against fluent interfaces, which
  44  would break an [access interceptor value holder](access-interceptor-value-holder.md) instead.
  45  
  46  ## Differences with [access interceptor value holder](access-interceptor-value-holder.md):
  47  
  48   * It does **NOT** implement the `ProxyManager\Proxy\ValueHolderInterface`, since the proxy itself
  49     does not keep a reference to the original object being proxied
  50   * In all interceptor methods (see [access interceptor value holder](access-interceptor-value-holder.md)),
  51     the `$instance` passed in is the proxy itself. There is no way  to gather a reference to the
  52     original object right now, and that's mainly to protect from misuse.
  53  
  54  ## Known limitations
  55  
  56   * It is **NOT** possible to intercept access to public properties
  57   * It is **NOT** possible to proxy interfaces, since this proxy relies on `parent::method()` calls.
  58     Interfaces obviously don't provide a parent method implementation.
  59   * calling `unset` on a property of an access interceptor scope localizer (or the real instance)
  60     will cause the two objects to be un-synchronized, with possible unexpected behaviour.
  61   * serializing or un-serializing an access interceptor scope localizer (or the real instance)
  62     will not cause the real instance (or the proxy) to be serialized or un-serialized
  63   * if a proxied object contains private properties, then an exception will be thrown if you use
  64     PHP `< 5.4.0`.
  65  
  66  ## Example
  67  
  68  Here's an example of how you can create and use an access interceptor scope localizer :
  69  
  70  ```php
  71  <?php
  72  
  73  use ProxyManager\Factory\AccessInterceptorScopeLocalizerFactory as Factory;
  74  
  75  require_once  __DIR__ . '/vendor/autoload.php';
  76  
  77  class Foo
  78  {
  79      public function doFoo()
  80      {
  81          echo "Foo!\n";
  82      }
  83  }
  84  
  85  $factory = new Factory();
  86  
  87  $proxy = $factory->createProxy(
  88      new Foo(),
  89      array('doFoo' => function () { echo "PreFoo!\n"; }),
  90      array('doFoo' => function () { echo "PostFoo!\n"; })
  91  );
  92  
  93  $proxy->doFoo();
  94  ```
  95  
  96  This send something like following to your output:
  97  
  98  ```
  99  PreFoo!
 100  Foo!
 101  PostFoo!
 102  ```
 103  
 104  This is pretty much the same logic that you can find
 105  in [access interceptor value holder](access-interceptor-value-holder.md).


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1