[ Index ]

PHP Cross Reference of phpBB-3.3.3-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/docs/ -> null-object.md (source)

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


Generated: Sun Feb 14 20:08:31 2021 Cross-referenced by PHPXref 0.7.1