Location: PHPKode > projects > Open Power Template > docs/Opt/api.opt-class.register.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="pl">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta name="robots" content="all" />

	<title>register() - Open Power Template</title>
	
	<link rel="stylesheet" type="text/css" href="design/generic.css" media="all"  />
	<link rel="stylesheet" type="text/css" href="design/print.css" media="print" />
	<!--[if lte IE 6]><link rel="stylesheet" href="design/ie.css" type="text/css" /><![endif]-->	
	<!--[if IE 7]><link rel="stylesheet" href="design/ie7.css" type="text/css" /><![endif]-->
</head>
<body>

<div id="wrap">
	<div id="header">
		<h1>Open Power Template 2.0</h1>
		<h2>register()</h2>
		<p class="generated">@ 02.09.2010</p>
		<p class="location"><a href="index.html"><strong>User manual</strong></a> &raquo; <a href="api.html">API Reference</a> &raquo; <a href="api.opt-class.html">Opt_Class</a> &raquo; <a href="api.opt-class.register.html">Opt_Class::register()</a></p>
	</div>
	
	<div id="content"><dl class="location"><dt><a href="api.opt-class.html">7.1. Opt_Class class</a><br/>7.1.6. register()</dt><dd class="prev">7.1.5. loadConfig()<br/><a href="api.opt-class.load-config.html">&laquo; Previous</a></dd><dd class="next">7.1.7. setBufferState()<br/><a href="api.opt-class.set-buffer-state.html">Next &raquo;</a></dd></dl>	<h1>7.1.6. register()</h1><div class="tf_reference"><table><tr><th>Construct</th><td>Method</td></tr><tr><th>Visibility</th><td>public</td></tr><tr><th>Reference</th><td><code>void register(int $type, mixed $name [, string $value])</code></td></tr><tr><th>Argument list</th><td><dl><dt><code>$type</code> <small>- int</small></dt><dd>The type of the registered item(s).</dd><dt><code>$name</code> <small>- mixed</small></dt><dd>The item name or the list of registered items.</dd><dt><code>$value</code> <small>- string</small></dt><dd>The extra item value used with some types.</dd></dl></td></tr><tr><th>Thrown exceptions</th><td><code>BadMethodCallException</code></td></tr></table><hr/></div><p>Registers a new add-on in OPT identified by <code>$type</code>. The next arguments depend on the specified type. The available types are:</p>

<ol>
<li><code>Opt_Class::OPT_INSTRUCTION</code> - new instruction processor with the specified <code>$name</code>.</li>
<li><code>Opt_Class::OPT_NAMESPACE</code> - new XML namespace recognized by OPT specified in <code>$name</code>.</li>
<li><code>Opt_Class::OPT_FORMAT</code> - new data format specified in <code>$name</code>.</li>
<li><code>Opt_Class::OPT_COMPONENT</code> - new component XML tag specified in <code>$name</code>. <code>$value</code> is the component class name.</li>
<li><code>Opt_Class::OPT_BLOCK</code> - new block XML tag specified in <code>$name</code>. <code>$value</code> is the block class name.</li>
<li><code>Opt_Class::PHP_FUNCTION</code> - new PHP function allowed to be used in templates. <code>$name</code> is the function name visible in templates and `$value - the real PHP function name.</li>
<li><code>Opt_Class::PHP_CLASS</code> - new PHP class allowed to be used in templates. <code>$name</code> is the class name visible in templates and <code>$value</code> - the real PHP class name.</li>
</ol>

<p>This method supports also mass registering of items by specifying an array as <code>$name</code>. For types from 1 to 3 it looks like this:</p>

<pre class="php"><span style="color: #000088;">$tpl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">register</span><span style="color: #009900;">&#40;</span>Opt_Class<span style="color: #339933;">::</span><span style="color: #004000;">OPT_INSTRUCTION</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'Instruction1'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'My_Instruction_1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Instruction2'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'My_Instruction_2'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Instruction3'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'My_Instruction_3'</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>

<p>For types from 4 to 7 we have to use associative array:</p>

<pre class="php"><span style="color: #000088;">$tpl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">register</span><span style="color: #009900;">&#40;</span>Opt_Class<span style="color: #339933;">::</span><span style="color: #004000;">OPT_COMPONENT</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'my:select'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'My_Select_Component'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'my:text'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'My_Text_Component'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'my:radio'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'My_Radio_Component'</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>

<blockquote class="error">
  <p>This method must be used before <a href="api.opt-class.setup.html" title="7.1.10. Opt_Class::setup()">Opt_Class::setup()</a> method.</p>
</blockquote>

<h2>Registering PHP functions and classes</h2>

<p>The templates do not support directly PHP namespaces, however the namespace can be specified during the function/class registration:</p>

<pre class="php"><span style="color: #000088;">$tpl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">register</span><span style="color: #009900;">&#40;</span>Opt_Class<span style="color: #339933;">::</span><span style="color: #004000;">PHP_CLASS</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'templateClass'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'namespace::myClass'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>

<p>In the same way we are allowed to register static class methods as functions.</p>

<p>For PHP functions, OPT introduces another interesting feature. Because the argument order in PHP library is quite messy and OPT follows strict rules, the compiler is able to change the order in the compile time, if we specify the additional rules. The order rules are specified before the real PHP function name and are enclosed within <code>#</code>:</p>

<pre class="php"><span style="color: #000088;">$tpl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">register</span><span style="color: #009900;">&#40;</span>Opt_Class<span style="color: #339933;">::</span><span style="color: #004000;">PHP_FUNCTION</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'regexReplace'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'#3,1,2#preg_replace'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>

<p>It could be read like this:</p>

<ol>
<li>The first argument in templates must be in the third place in the compiled template.</li>
<li>The second argument - in the first one.</li>
<li>The third one - in the second.</li>
</ol>

<p>We may also specify some optional values:</p>

<pre class="php"><span style="color: #000088;">$tpl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">register</span><span style="color: #009900;">&#40;</span>Opt_Class<span style="color: #339933;">::</span><span style="color: #004000;">PHP_FUNCTION</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'foo'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'#3,1,2:null#foo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>

<p>In this case, the third argument in templates is optional, but in PHP it is required. We must specify the default value (<strong>null</strong> in this case) for the compiler then by adding <code>:null</code> to the position number.</p>

<h2>Registering instructions and formats</h2>

<p>For instructions and processors we register their class names and the identifiers they will appear in the parser. If we specify the identifier only, OPT will construct a class name for them, accordingly <code>Opt_Instruction_Identifier</code> and <code>Opt_Format_Identifier</code>. Sometimes we may want to give the classes different names, for example due to the autoloading purposes. In this case, we specify the class as the third argument.</p>

<pre class="php"><span style="color: #000088;">$tpl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">register</span><span style="color: #009900;">&#40;</span>Opt_Class<span style="color: #339933;">::</span><span style="color: #004000;">OPT_FORMAT</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Foo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">// Registers &quot;Foo&quot; from &quot;Opt_Format_Foo&quot; class</span>
<span style="color: #000088;">$tpl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">register</span><span style="color: #009900;">&#40;</span>Opt_Class<span style="color: #339933;">::</span><span style="color: #004000;">OPT_FORMAT</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Foo'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Some_Class'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Registers &quot;Foo&quot; from &quot;Some_Class&quot;</span></pre>

<blockquote class="warning">
  <p>When registering a group of instructions or formats with a single call of <code>register()</code>, you are obliged to provide assotiative arrays, where the keys are identifiers, and the values - the class names. OPT does not expand the identifiers to the class names in this case.</p>
</blockquote>
<dl class="location location-bottom"><dt>7.1.6. register()<br/><a href="api.opt-class.html">7.1. Opt_Class class</a></dt><dd class="prev"><a href="api.opt-class.load-config.html">&laquo; Previous</a><br/>7.1.5. loadConfig()</dd><dd class="next"><a href="api.opt-class.set-buffer-state.html">Next &raquo;</a><br/>7.1.7. setBufferState()</dd></dl>		</div>
	
	<div id="footer">
		<p>Copyright &copy; <a href="http://www.invenzzia.org/">Invenzzia Group 2008-2009</a></p>
		<p>Available under the terms of license: <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License 1.2</a></p>
		<p>Generated by <strong>TypeFriendly 0.1.4</strong> by <a href="http://www.invenzzia.org/">Invenzzia</a></p>
	</div>
</div>

</body>
</html>
Return current item: Open Power Template