[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

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

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


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1