Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
access-interceptor-scope-localizer.md
001 ---
002 title: Access Interceptor Scope Localizer Proxy
003 ---
004
005 # Access Interceptor Scope Localizer Proxy
006
007 An access interceptor scope localizer is a smart reference proxy that allows you to dynamically
008 define logic to be executed before or after any of the proxied object's methods' logic.
009
010 It works exactly like the [access interceptor value holder](access-interceptor-value-holder.md),
011 with some minor differences in behavior.
012
013 The working concept of an access interceptor scope localizer is to localize scope of a proxied object:
014
015 ```php
016 class Example
017 {
018 protected $foo;
019 protected $bar;
020 protected $baz;
021
022 public function doFoo()
023 {
024 // ...
025 }
026 }
027
028 class ExampleProxy extends Example
029 {
030 public function __construct(Example $example)
031 {
032 $this->foo = & $example->foo;
033 $this->bar = & $example->bar;
034 $this->baz = & $example->baz;
035 }
036
037 public function doFoo()
038 {
039 return parent::doFoo();
040 }
041 }
042 ```
043
044 This allows to create a mirror copy of the real instance, where any change in the proxy or in the real
045 instance is reflected in both objects.
046
047 The main advantage of this approach is that the proxy is now safe against fluent interfaces, which
048 would break an [access interceptor value holder](access-interceptor-value-holder.md) instead.
049
050 ## Differences with [access interceptor value holder](access-interceptor-value-holder.md):
051
052 * It does **NOT** implement the `ProxyManager\Proxy\ValueHolderInterface`, since the proxy itself
053 does not keep a reference to the original object being proxied
054 * In all interceptor methods (see [access interceptor value holder](access-interceptor-value-holder.md)),
055 the `$instance` passed in is the proxy itself. There is no way to gather a reference to the
056 original object right now, and that's mainly to protect from misuse.
057
058 ## Known limitations
059
060 * It is **NOT** possible to intercept access to public properties
061 * It is **NOT** possible to proxy interfaces, since this proxy relies on `parent::method()` calls.
062 Interfaces obviously don't provide a parent method implementation.
063 * calling `unset` on a property of an access interceptor scope localizer (or the real instance)
064 will cause the two objects to be un-synchronized, with possible unexpected behaviour.
065 * serializing or un-serializing an access interceptor scope localizer (or the real instance)
066 will not cause the real instance (or the proxy) to be serialized or un-serialized
067 * methods using `func_get_args()`, `func_get_arg()` and `func_num_arg()` will not function properly
068 for parameters that are not part of the proxied object interface: use
069 [variadic arguments](http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list)
070 instead.
071
072 ## Example
073
074 Here's an example of how you can create and use an access interceptor scope localizer :
075
076 ```php
077 <?php
078
079 use ProxyManager\Factory\AccessInterceptorScopeLocalizerFactory as Factory;
080
081 require_once __DIR__ . '/vendor/autoload.php';
082
083 class Foo
084 {
085 public function doFoo()
086 {
087 echo "Foo!\n";
088 }
089 }
090
091 $factory = new Factory();
092
093 $proxy = $factory->createProxy(
094 new Foo(),
095 ['doFoo' => function () { echo "PreFoo!\n"; }],
096 ['doFoo' => function () { echo "PostFoo!\n"; }]
097 );
098
099 $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).
112