[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
1 # Null Object Proxy 2 3 A Null Object proxy is a [null object pattern](http://en.wikipedia.org/wiki/Null_Object_pattern) implementation. 4 The proxy factory creates a new object with defined neutral behavior based on an other object, class name or interface. 5 6 ## What is null object proxy ? 7 8 In your application, when you can't return the object related to the request, the consumer of the model must check 9 for the return value and handle the failing condition gracefully, thus generating an explosion of conditionals throughout your code. 10 Fortunately, this seemingly-tangled situation can be sorted out simply by creating a polymorphic implementation of the 11 domain object, which would implement the same interface as the one of the object in question, only that its methods 12 wouldn’t do anything, therefore offloading client code from doing repetitive checks for ugly null values when the operation 13 is executed. 14 15 ## Usage examples 16 17 ```php 18 class UserMapper 19 { 20 private $adapter; 21 22 public function __construct(DatabaseAdapterInterface $adapter) { 23 $this->adapter = $adapter; 24 } 25 26 public function fetchById($id) { 27 $this->adapter->select("users", array("id" => $id)); 28 if (!$row = $this->adapter->fetch()) { 29 return null; 30 } 31 return $this->createUser($row); 32 } 33 34 private function createUser(array $row) { 35 $user = new Entity\User($row["name"], $row["email"]); 36 $user->setId($row["id"]); 37 return $user; 38 } 39 } 40 ``` 41 42 If you want to remove conditionals from client code, you need to have a version of the entity conforming to the corresponding 43 interface. With the Null Object Proxy, you can build this object : 44 45 ```php 46 $factory = new \ProxyManager\Factory\NullObjectFactory(); 47 48 $nullUser = $factory->createProxy('Entity\User'); 49 50 var_dump($nullUser->getName()); // empty return 51 ``` 52 53 You can now return a valid entity : 54 55 ```php 56 class UserMapper 57 { 58 private $adapter; 59 60 public function __construct(DatabaseAdapterInterface $adapter) { 61 $this->adapter = $adapter; 62 } 63 64 public function fetchById($id) { 65 $this->adapter->select("users", array("id" => $id)); 66 return $this->createUser($this->adapter->fetch()); 67 } 68 69 private function createUser($row) { 70 if (!$row) { 71 $factory = new \ProxyManager\Factory\NullObjectFactory(); 72 73 return $factory->createProxy('Entity\User'); 74 } 75 $user = new Entity\User($row["name"], $row["email"]); 76 $user->setId($row["id"]); 77 return $user; 78 } 79 } 80 ``` 81 82 ## Proxying interfaces 83 84 You can also generate proxies from an interface FQCN. By proxying an interface, you will only be able to access the 85 methods defined by the interface itself, and like with the object, the methods are empty. 86 87 ## Tuning performance for production 88 89 See [Tuning ProxyManager for Production](https://github.com/Ocramius/ProxyManager/blob/master/docs/tuning-for-production.md).
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |