[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

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

   1  <!DOCTYPE html>
   2  <html class="no-js" id="top">
   3  <head>
   4      <title>ProxyManager - Null object</title>
   5  
   6      <meta name="description" content="A proxyManager write in php" />
   7      <meta name="keywords" content="ProxyManager, proxy, manager, ocramius, Marco Pivetta, php, null object" />
   8      <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
   9      <link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:200,300,400,600' rel='stylesheet' type='text/css'>
  10      <link href="css/styles.css" rel="stylesheet" />
  11      <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/styles/default.min.css">
  12      <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/highlight.min.js"></script>
  13      <script>hljs.initHighlightingOnLoad();</script>
  14      <link rel="shortcut icon" href="favicon.ico">
  15  </head>
  16  <body>
  17  
  18  <header class="site-header">
  19  <div class="container">
  20  <h1><a href="index.html"><img alt="ProxyManager" src="img/block.png" /></a></h1>
  21  
  22  <nav class="main-nav" role="navigation">
  23  <ul>
  24      <li><a href="https://github.com/Ocramius/ProxyManager" target="_blank">Github</a>
  25      <div class="bcms-clearfix"></div>
  26  </li>
  27  </ul>
  28  </nav>
  29  </div>
  30  </header>
  31  <main role="main">
  32  <section class="component-content">
  33  
  34  <div class="component-demo" id="live-demo">
  35      <div class="container">
  36              <div class="main-wrapper" style="text-align: right">
  37                  <iframe src="http://ghbtns.com/github-btn.html?user=ocramius&amp;repo=ProxyManager&amp;type=fork&amp;count=true&amp;size=large"
  38    allowtransparency="true" frameborder="0" scrolling="0" width="310" height="40"></iframe>
  39  
  40                  <iframe src="http://ghbtns.com/github-btn.html?user=ocramius&amp;repo=ProxyManager&amp;type=watch&amp;count=true&amp;size=large"
  41    allowtransparency="true" frameborder="0" scrolling="0" width="200" height="40"></iframe>
  42  
  43              </div>
  44          <div class="bcms-clearfix bcms-clearfix"></div>
  45      </div>
  46  </div>
  47  <div class="component-info">
  48  <div class="container">
  49  <aside class="sidebar">
  50      <nav class="spy-nav">
  51          <ul>
  52              <li><a href="index.html">Intro</a></li>
  53              <li><a href="virtual-proxy.html">Virtual Proxy</a></li>
  54              <li><a href="null-object.html">Null Objects</a></li>
  55              <li><a href="ghost-object.html">Ghost Objects</a></li>
  56              <li><a href="remote-object.html">Remote Object</a></li>
  57              <li><a href="contributing.html">Contributing</a></li>
  58              <li><a href="credits.html">Credits</a></li>
  59              <li><a href="copyright.html">Copyright</a></li>
  60          </ul>
  61      </nav>
  62  <div class="bcms-clearfix bcms-clearfix"></div>
  63  <a class="btn btn-action btn-full download-component"
  64      href="download.html">Download</a>
  65      <div class="bcms-clearfix"></div>
  66  </aside>
  67  
  68  <div class="content">
  69      <div class="bcms-clearfix"></div>
  70      <h3 class="section-title">Null Object Proxy</h3>
  71      <p>A Null Object proxy is a <a href="http://en.wikipedia.org/wiki/Null_Object_pattern" target="_blank">null object pattern</a> implementation. The proxy factory creates a new object with defined neutral behavior based on an other object, class name or interface.</p>
  72  <hr />
  73  
  74      <h3 class="section-title">What is null object proxy ?</h3>
  75  
  76      <p>In your application, when you can't return the object related to the request, the consumer of the model must check for the return value and handle the failing condition gracefully, thus generating an explosion of conditionals throughout your code. Fortunately, this seemingly-tangled situation can be sorted out simply by creating a polymorphic implementation of the domain object, which would implement the same interface as the one of the object in question, only that its methods wouldn’t do anything, therefore offloading client code from doing repetitive checks for ugly null values when the operation is executed.</p>
  77  
  78  <hr />
  79  
  80      <h3 class="section-title">Usage examples</h3>
  81  
  82      <pre>
  83          <code class="php">
  84  class UserMapper
  85  {   
  86      private $adapter;
  87  
  88      public function __construct(DatabaseAdapterInterface $adapter) {
  89          $this->adapter = $adapter;
  90      }
  91  
  92      public function fetchById($id) {
  93          $this->adapter->select("users", array("id" => $id));
  94          if (!$row = $this->adapter->fetch()) {
  95              return null;
  96          }
  97          return $this->createUser($row);
  98      }
  99  
 100      private function createUser(array $row) {
 101          $user = new Entity\User($row["name"], $row["email"]);
 102          $user->setId($row["id"]);
 103          return $user;
 104      }
 105  }
 106          </code>
 107      </pre>
 108  
 109      <p>If you want to remove conditionals from client code, you need to have a version of the entity conforming to the corresponding interface. With the Null Object Proxy, you can build this object :</p>
 110  
 111      <pre>
 112          <code class="php">
 113  $factory = new \ProxyManager\Factory\NullObjectFactory();
 114  
 115  $nullUser = $factory->createProxy('Entity\User');
 116  
 117  var_dump($nullUser->getName()); // empty return

 118          </code>
 119      </pre>
 120  
 121      <p>You can now return a valid entity :</p>
 122  
 123      <pre>
 124          <code class="php">
 125  class UserMapper
 126  {   
 127      private $adapter;
 128  
 129      public function __construct(DatabaseAdapterInterface $adapter) {
 130          $this->adapter = $adapter;
 131      }
 132  
 133      public function fetchById($id) {
 134          $this->adapter->select("users", array("id" => $id));
 135          return $this->createUser($this->adapter->fetch());
 136      }
 137  
 138      private function createUser($row) {
 139          if (!$row) {
 140              $factory = new \ProxyManager\Factory\NullObjectFactory();
 141  
 142              return $factory->createProxy('Entity\User');
 143          }
 144          $user = new Entity\User($row["name"], $row["email"]);
 145          $user->setId($row["id"]);
 146          return $user; 
 147      }
 148  }
 149          </code>
 150      </pre>
 151      
 152      
 153  <hr />
 154  
 155      <h3 class="section-title">Proxying interfaces</h3>
 156  
 157      <p>You can also generate proxies from an interface FQCN. By proxying an interface, you will only be able to access the methods defined by the interface itself, and like with the object, the methods are empty.</p>
 158  
 159      <p>Tuning performance for production</p>
 160  
 161      <p>See <a href="production.html">Tuning ProxyManager for Production.</a></p>
 162  
 163  </main>
 164  
 165      <footer class="site-footer" role="contentinfo">
 166          <div class="container">
 167              <div class="footer-logos">
 168                  <ul>
 169                      <li><a href="index.html">Intro</a> | </li>
 170                      <li><a href="virtual-proxy.html">Virtual Proxy</a> | </li>
 171                      <li><a href="null-object.html">Null Objects</a> | </li>
 172                      <li><a href="ghost-object.html">Ghost Objects</a> | </li>
 173                      <li><a href="remote-object.html">Remote Object</a> | </li>
 174                      <li><a href="contributing.html">Contributing</a> | </li>
 175                      <li><a href="credits.html">Credits</a> | </li>
 176                      <li><a href="copyright.html">Copyright</a></li>
 177                  </ul>
 178              </div>
 179          </div>
 180  
 181          <div class="bcms-clearfix"></div>
 182      </footer>
 183      <div class="bcms-clearfix"></div>
 184      </body>
 185  </html>


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1