Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 13023

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Emit callbacks</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="yaml.callbacks.parse.html">Parse callbacks</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.yaml.html">Yaml Functions</a></div>
 <div class="up"><a href="yaml.callbacks.html">Callbacks</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="yaml.callbacks.emit" class="section">
  <h2 class="title">Emit callbacks</h2>
  <p class="para">
   Emit callbacks are invoked when an instance of a registered class is
   emitted by  <span class="function"><a href="function.yaml-emit.html" class="function">yaml_emit()</a></span> or
    <span class="function"><a href="function.yaml-emit-file.html" class="function">yaml_emit_file()</a></span>. The callback is passed the object to
   be emitted. The callback must return an array having two keys:
   &quot;<em>tag</em>&quot; and &quot;<em>data</em>&quot;.
   The value associated with the &quot;<em>tag</em>&quot; key must
   be a string to be used as the YAML tag in the output. The value associated
   with the &quot;<em>data</em>&quot; key will be encoded as YAML
   and emitted in place of the intercepted object.
  </p>
  <div class="example" id="example-4100">
   <p><strong>Example #1 Emit callback example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">EmitExample&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;data&nbsp;may&nbsp;be&nbsp;in&nbsp;any&nbsp;pecl/yaml&nbsp;suitable&nbsp;type<br /><br />&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$d</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;</span><span style="color: #FF8000">/**<br />&nbsp;&nbsp;&nbsp;*&nbsp;Yaml&nbsp;emit&nbsp;callback&nbsp;function,&nbsp;referred&nbsp;on&nbsp;yaml_emit&nbsp;call&nbsp;by&nbsp;class&nbsp;name.<br />&nbsp;&nbsp;&nbsp;*<br />&nbsp;&nbsp;&nbsp;*&nbsp;Expected&nbsp;to&nbsp;return&nbsp;an&nbsp;array&nbsp;with&nbsp;2&nbsp;values:<br />&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;-&nbsp;'tag':&nbsp;custom&nbsp;tag&nbsp;for&nbsp;this&nbsp;serialization<br />&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;-&nbsp;'data':&nbsp;value&nbsp;to&nbsp;convert&nbsp;to&nbsp;yaml&nbsp;(array,&nbsp;string,&nbsp;bool,&nbsp;number)<br />&nbsp;&nbsp;&nbsp;*<br />&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;object&nbsp;$obj&nbsp;Object&nbsp;to&nbsp;be&nbsp;emitted<br />&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;array&nbsp;Tag&nbsp;and&nbsp;surrogate&nbsp;data&nbsp;to&nbsp;emit<br />&nbsp;&nbsp;&nbsp;*/<br />&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;static&nbsp;function&nbsp;</span><span style="color: #0000BB">yamlEmit&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">EmitExample&nbsp;$obj</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'tag'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'!example/emit'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'data'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;);<br />&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$emit_callbacks&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;</span><span style="color: #DD0000">'EmitExample'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'EmitExample'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'yamlEmit'</span><span style="color: #007700">)<br />);<br /><br /></span><span style="color: #0000BB">$t&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EmitExample</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'a'</span><span style="color: #007700">,</span><span style="color: #DD0000">'b'</span><span style="color: #007700">,</span><span style="color: #DD0000">'c'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$yaml&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">yaml_emit</span><span style="color: #007700">(<br />&nbsp;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'example'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$t</span><span style="color: #007700">,<br />&nbsp;&nbsp;),<br />&nbsp;&nbsp;</span><span style="color: #0000BB">YAML_ANY_ENCODING</span><span style="color: #007700">,<br />&nbsp;&nbsp;</span><span style="color: #0000BB">YAML_ANY_BREAK</span><span style="color: #007700">,<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$emit_callbacks<br /></span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$yaml</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>The above example will output
something similar to:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
string(43) &quot;---
example: !example/emit
- a
- b
- c
...
&quot;
</pre></div>
   </div>
  </div>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="yaml.callbacks.parse.html">Parse callbacks</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.yaml.html">Yaml Functions</a></div>
 <div class="up"><a href="yaml.callbacks.html">Callbacks</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>