2023-01-23 11:03:31 +01:00

186 lines
6.9 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html class="no-js" id="top">
<head>
<title>ProxyManager - Null object</title>
<meta name="description" content="A proxyManager write in php" />
<meta name="keywords" content="ProxyManager, proxy, manager, ocramius, Marco Pivetta, php, null object" />
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:200,300,400,600' rel='stylesheet' type='text/css'>
<link href="css/styles.css" rel="stylesheet" />
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/styles/default.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<link rel="shortcut icon" href="favicon.ico">
</head>
<body>
<header class="site-header">
<div class="container">
<h1><a href="index.html"><img alt="ProxyManager" src="img/block.png" /></a></h1>
<nav class="main-nav" role="navigation">
<ul>
<li><a href="https://github.com/Ocramius/ProxyManager" target="_blank">Github</a>
<div class="bcms-clearfix"></div>
</li>
</ul>
</nav>
</div>
</header>
<main role="main">
<section class="component-content">
<div class="component-demo" id="live-demo">
<div class="container">
<div class="main-wrapper" style="text-align: right">
<iframe src="http://ghbtns.com/github-btn.html?user=ocramius&amp;repo=ProxyManager&amp;type=fork&amp;count=true&amp;size=large"
allowtransparency="true" frameborder="0" scrolling="0" width="310" height="40"></iframe>
<iframe src="http://ghbtns.com/github-btn.html?user=ocramius&amp;repo=ProxyManager&amp;type=watch&amp;count=true&amp;size=large"
allowtransparency="true" frameborder="0" scrolling="0" width="200" height="40"></iframe>
</div>
<div class="bcms-clearfix bcms-clearfix"></div>
</div>
</div>
<div class="component-info">
<div class="container">
<aside class="sidebar">
<nav class="spy-nav">
<ul>
<li><a href="index.html">Intro</a></li>
<li><a href="virtual-proxy.html">Virtual Proxy</a></li>
<li><a href="null-object.html">Null Objects</a></li>
<li><a href="ghost-object.html">Ghost Objects</a></li>
<li><a href="remote-object.html">Remote Object</a></li>
<li><a href="contributing.html">Contributing</a></li>
<li><a href="credits.html">Credits</a></li>
<li><a href="copyright.html">Copyright</a></li>
</ul>
</nav>
<div class="bcms-clearfix bcms-clearfix"></div>
<a class="btn btn-action btn-full download-component"
href="download.html">Download</a>
<div class="bcms-clearfix"></div>
</aside>
<div class="content">
<div class="bcms-clearfix"></div>
<h3 class="section-title">Null Object Proxy</h3>
<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>
<hr />
<h3 class="section-title">What is null object proxy ?</h3>
<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 wouldnt do anything, therefore offloading client code from doing repetitive checks for ugly null values when the operation is executed.</p>
<hr />
<h3 class="section-title">Usage examples</h3>
<pre>
<code class="php">
class UserMapper
{
private $adapter;
public function __construct(DatabaseAdapterInterface $adapter) {
$this->adapter = $adapter;
}
public function fetchById($id) {
$this->adapter->select("users", array("id" => $id));
if (!$row = $this->adapter->fetch()) {
return null;
}
return $this->createUser($row);
}
private function createUser(array $row) {
$user = new Entity\User($row["name"], $row["email"]);
$user->setId($row["id"]);
return $user;
}
}
</code>
</pre>
<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>
<pre>
<code class="php">
$factory = new \ProxyManager\Factory\NullObjectFactory();
$nullUser = $factory->createProxy('Entity\User');
var_dump($nullUser->getName()); // empty return
</code>
</pre>
<p>You can now return a valid entity :</p>
<pre>
<code class="php">
class UserMapper
{
private $adapter;
public function __construct(DatabaseAdapterInterface $adapter) {
$this->adapter = $adapter;
}
public function fetchById($id) {
$this->adapter->select("users", array("id" => $id));
return $this->createUser($this->adapter->fetch());
}
private function createUser($row) {
if (!$row) {
$factory = new \ProxyManager\Factory\NullObjectFactory();
return $factory->createProxy('Entity\User');
}
$user = new Entity\User($row["name"], $row["email"]);
$user->setId($row["id"]);
return $user;
}
}
</code>
</pre>
<hr />
<h3 class="section-title">Proxying interfaces</h3>
<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>
<p>Tuning performance for production</p>
<p>See <a href="production.html">Tuning ProxyManager for Production.</a></p>
</main>
<footer class="site-footer" role="contentinfo">
<div class="container">
<div class="footer-logos">
<ul>
<li><a href="index.html">Intro</a> | </li>
<li><a href="virtual-proxy.html">Virtual Proxy</a> | </li>
<li><a href="null-object.html">Null Objects</a> | </li>
<li><a href="ghost-object.html">Ghost Objects</a> | </li>
<li><a href="remote-object.html">Remote Object</a> | </li>
<li><a href="contributing.html">Contributing</a> | </li>
<li><a href="credits.html">Credits</a> | </li>
<li><a href="copyright.html">Copyright</a></li>
</ul>
</div>
</div>
<div class="bcms-clearfix"></div>
</footer>
<div class="bcms-clearfix"></div>
</body>
</html>