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 |
null-object.md
001 ---
002 title: Null Object Proxy
003 ---
004
005 # Null Object Proxy
006
007 A Null Object proxy is a [null object pattern](http://en.wikipedia.org/wiki/Null_Object_pattern) implementation.
008 The proxy factory creates a new object with defined neutral behavior based on an other object, class name or interface.
009
010 ## What is null object proxy ?
011
012 In your application, when you can't return the object related to the request, the consumer of the model must check
013 for the return value and handle the failing condition gracefully, thus generating an explosion of conditionals throughout your code.
014 Fortunately, this seemingly-tangled situation can be sorted out simply by creating a polymorphic implementation of the
015 domain object, which would implement the same interface as the one of the object in question, only that its methods
016 wouldn't do anything, therefore offloading client code from doing repetitive checks for ugly null values when the operation
017 is executed.
018
019 ## Usage examples
020
021 ```php
022 class UserMapper
023 {
024 private $adapter;
025
026 public function __construct(DatabaseAdapterInterface $adapter) {
027 $this->adapter = $adapter;
028 }
029
030 public function fetchById($id) {
031 $this->adapter->select('users', ['id' => $id]);
032
033 if (!$row = $this->adapter->fetch()) {
034 return null;
035 }
036
037 return $this->createUser($row);
038 }
039
040 private function createUser(array $row) {
041 $user = new Entity\User($row['name'], $row['email']);
042
043 $user->setId($row['id']);
044
045 return $user;
046 }
047 }
048 ```
049
050 If you want to remove conditionals from client code, you need to have a version of the entity conforming to the corresponding
051 interface. With the Null Object Proxy, you can build this object :
052
053 ```php
054 $factory = new \ProxyManager\Factory\NullObjectFactory();
055
056 $nullUser = $factory->createProxy('Entity\User');
057
058 var_dump($nullUser->getName()); // empty return
059 ```
060
061 You can now return a valid entity :
062
063 ```php
064 class UserMapper
065 {
066 private $adapter;
067
068 public function __construct(DatabaseAdapterInterface $adapter) {
069 $this->adapter = $adapter;
070 }
071
072 public function fetchById($id) {
073 $this->adapter->select('users', ['id' => $id]);
074
075 return $this->createUser($this->adapter->fetch());
076 }
077
078 private function createUser($row) {
079 if (!$row) {
080 $factory = new \ProxyManager\Factory\NullObjectFactory();
081
082 return $factory->createProxy('Entity\User');
083 }
084
085 $user = new Entity\User($row['name'], $row['email']);
086
087 $user->setId($row['id']);
088
089 return $user;
090 }
091 }
092 ```
093
094 ## Proxying interfaces
095
096 You can also generate proxies from an interface FQCN. By proxying an interface, you will only be able to access the
097 methods defined by the interface itself, and like with the object, the methods are empty.
098
099 ## Tuning performance for production
100
101 See [Tuning ProxyManager for Production](tuning-for-production.md).
102