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-value-holder.md
001 ---
002 title: Access Interceptor Value Holder Proxy
003 ---
004
005 # Access Interceptor Value Holder Proxy
006
007 An access interceptor value holder is a smart reference proxy that allows you to dynamically
008 define logic to be executed before or after any of the wrapped object's methods
009 logic.
010
011 It wraps around a real instance of the object to be proxied, and can be useful for things like:
012
013 * caching execution of slow and heavy methods
014 * log method calls
015 * debugging
016 * event triggering
017 * handling of orthogonal logic, and [AOP](http://en.wikipedia.org/wiki/Aspect-oriented_programming) in general
018
019 ## Example
020
021 Here's an example of how you can create and use an access interceptor value holder:
022
023 ```php
024 <?php
025
026 use ProxyManager\Factory\AccessInterceptorValueHolderFactory as Factory;
027
028 require_once __DIR__ . '/vendor/autoload.php';
029
030 class Foo
031 {
032 public function doFoo()
033 {
034 echo "Foo!\n";
035 }
036 }
037
038 $factory = new Factory();
039
040 $proxy = $factory->createProxy(
041 new Foo(),
042 ['doFoo' => function () { echo "PreFoo!\n"; }],
043 ['doFoo' => function () { echo "PostFoo!\n"; }]
044 );
045
046 $proxy->doFoo();
047 ```
048
049 This send something like following to your output:
050
051 ```
052 PreFoo!
053 Foo!
054 PostFoo!
055 ```
056
057 ## Implementing pre- and post- access interceptors
058
059 A proxy produced by the
060 [`ProxyManager\Factory\AccessInterceptorValueHolderFactory`](https://github.com/Ocramius/ProxyManager/blob/master/src/ProxyManager/Factory/AccessInterceptorValueHolderFactory.php)
061 implements the
062 [`ProxyManager\Proxy\AccessInterceptorValueHolderInterface`](https://github.com/Ocramius/ProxyManager/blob/master/src/ProxyManager/Proxy/AccessInterceptorValueHolderInterface.php).
063
064 Therefore, you can set an access interceptor callback by calling:
065
066 ```php
067 $proxy->setMethodPrefixInterceptor('methodName', function () { echo 'pre'; });
068 $proxy->setMethodSuffixInterceptor('methodName', function () { echo 'post'; });
069 ```
070
071 You can also listen to public properties access by attaching interceptors to `__get`, `__set`, `__isset` and `__unset`.
072
073 A prefix interceptor (executed before method logic) should have the following signature:
074
075 ```php
076 /**
077 * @var object $proxy the proxy that intercepted the method call
078 * @var object $instance the wrapped instance within the proxy
079 * @var string $method name of the called method
080 * @var array $params sorted array of parameters passed to the intercepted
081 * method, indexed by parameter name
082 * @var bool $returnEarly flag to tell the interceptor proxy to return early, returning
083 * the interceptor's return value instead of executing the method logic
084 *
085 * @return mixed
086 */
087 $prefixInterceptor = function ($proxy, $instance, $method, $params, & $returnEarly) {};
088 ```
089
090 A suffix interceptor (executed after method logic) should have the following signature:
091
092 ```php
093 /**
094 * @var object $proxy the proxy that intercepted the method call
095 * @var object $instance the wrapped instance within the proxy
096 * @var string $method name of the called method
097 * @var array $params sorted array of parameters passed to the intercepted
098 * method, indexed by parameter name
099 * @var mixed $returnValue the return value of the intercepted method
100 * @var bool $returnEarly flag to tell the proxy to return early, returning the interceptor's
101 * return value instead of the value produced by the method
102 *
103 * @return mixed
104 */
105 $suffixInterceptor = function ($proxy, $instance, $method, $params, $returnValue, & $returnEarly) {};
106 ```
107
108 ## Known limitations
109
110 * methods using `func_get_args()`, `func_get_arg()` and `func_num_arg()` will not function properly
111 for parameters that are not part of the proxied object interface: use
112 [variadic arguments](http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list)
113 instead.
114
115 ## Tuning performance for production
116
117 See [Tuning ProxyManager for Production](https://github.com/Ocramius/ProxyManager/blob/master/docs/tuning-for-production.md).
118