<!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: "<em>tag</em>" and "<em>data</em>". The value associated with the "<em>tag</em>" key must be a string to be used as the YAML tag in the output. The value associated with the "<em>data</em>" 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"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">EmitExample </span><span style="color: #007700">{<br /> public </span><span style="color: #0000BB">$data</span><span style="color: #007700">; </span><span style="color: #FF8000">// data may be in any pecl/yaml suitable type<br /><br /> </span><span style="color: #007700">public function </span><span style="color: #0000BB">__construct </span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">data </span><span style="color: #007700">= </span><span style="color: #0000BB">$d</span><span style="color: #007700">;<br /> }<br /><br /> </span><span style="color: #FF8000">/**<br /> * Yaml emit callback function, referred on yaml_emit call by class name.<br /> *<br /> * Expected to return an array with 2 values:<br /> * - 'tag': custom tag for this serialization<br /> * - 'data': value to convert to yaml (array, string, bool, number)<br /> *<br /> * @param object $obj Object to be emitted<br /> * @return array Tag and surrogate data to emit<br /> */<br /> </span><span style="color: #007700">public static function </span><span style="color: #0000BB">yamlEmit </span><span style="color: #007700">(</span><span style="color: #0000BB">EmitExample $obj</span><span style="color: #007700">) {<br /> return array(<br /> </span><span style="color: #DD0000">'tag' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'!example/emit'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'data' </span><span style="color: #007700">=> </span><span style="color: #0000BB">$obj</span><span style="color: #007700">-></span><span style="color: #0000BB">data</span><span style="color: #007700">,<br /> );<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$emit_callbacks </span><span style="color: #007700">= array(<br /> </span><span style="color: #DD0000">'EmitExample' </span><span style="color: #007700">=> array(</span><span style="color: #DD0000">'EmitExample'</span><span style="color: #007700">, </span><span style="color: #DD0000">'yamlEmit'</span><span style="color: #007700">)<br />);<br /><br /></span><span style="color: #0000BB">$t </span><span style="color: #007700">= new </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 </span><span style="color: #007700">= </span><span style="color: #0000BB">yaml_emit</span><span style="color: #007700">(<br /> array(<br /> </span><span style="color: #DD0000">'example' </span><span style="color: #007700">=> </span><span style="color: #0000BB">$t</span><span style="color: #007700">,<br /> ),<br /> </span><span style="color: #0000BB">YAML_ANY_ENCODING</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">YAML_ANY_BREAK</span><span style="color: #007700">,<br /> </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">?></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) "--- example: !example/emit - a - b - c ... " </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>