sdo.sample.reflection.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 98 行

HTML
98
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Reflecting on Service Data Objects</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="sdo.examples.html">Examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.sdo.html">SDO Functions</a></div> <div class="up"><a href="sdo.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="sdo.sample.reflection" class="section">  <h2 class="title">Reflecting on Service Data Objects</h2>  <p class="para">   SDOs have a knowledge of the structure they have been created to    represent (the model).  For example, a Company SDO created using    <a href="sdo.examples.html" class="link">the Company XML schema</a> above   would only be permitted to contain DepartmentType data objects    which in turn could only contain EmployeeType data objects.  </p>  <p class="para">   Sometimes it is useful to be able to access this model information at    runtime.  For example, this could be used to automatically generate    a user interface for populating a data object.  The model information    is accessed using reflection.    </p>  <p class="para">   <div class="example">    <p><b>Example #1 Reflecting on a Data Object</b></p>    <div class="example-contents"><p>     The following example shows how we can reflect on an empty Employee      data object.    </p></div>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Create&nbsp;the&nbsp;employee&nbsp;data&nbsp;object&nbsp;(e.g.&nbsp;from&nbsp;an&nbsp;XML&nbsp;Data&nbsp;Access&nbsp;Service)<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$employee&nbsp;</span><span style="color: #007700">=&nbsp;...;<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$reflection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SDO_Model_ReflectionDataObject</span><span style="color: #007700">(</span><span style="color: #0000BB">$employee</span><span style="color: #007700">);<br />&nbsp;&nbsp;print(</span><span style="color: #0000BB">$reflection</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:</p></div>    <div class="example-contents"><pre><div class="cdata"><pre>object(SDO_Model_ReflectionDataObject)#4 { - ROOT OBJECT - Type { companyNS:EmployeeType[3] { commonj.sdo:String $name; commonj.sdo:String $SN; commonj.sdo:Boolean $manager; } }</pre></div>   </pre></div>   <div class="example-contents"><p>     Using print on the SDO_Model_ReflectionDataObject writes out the data      object&#039;s model.  We can see from the output how the type      companyNS:EmployeeType has three properties and we can see the names      of the properties along with their types.  Note, the primitive types     are listed as SDO types (e.g. commonj.sdo namespace, String type).       It is worth noting that this is the SDO model and when these are      surfaced to an application they can be treated as the PHP equivalent      types (e.g. string and boolean).    </p></div>   </div>  </p>  <p class="para">   <div class="example">    <p><b>Example #2 Accessing the type information</b></p>    <div class="example-contents"><p>     We can query the type information of a data object using reflection.       The following example checks the type corresponds to a data object     rather than a primitive and then iterates through the properties of     the type, writing out the name of each property ($type and $property      are SDO_Model_Type and SDO_Model_Property objects, respectively).    </p></div>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Create&nbsp;the&nbsp;employee&nbsp;data&nbsp;object&nbsp;(e.g.&nbsp;from&nbsp;an&nbsp;XML&nbsp;Data&nbsp;Access&nbsp;Service)<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$employee&nbsp;</span><span style="color: #007700">=&nbsp;...;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$reflection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SDO_Model_ReflectionDataObject</span><span style="color: #007700">(</span><span style="color: #0000BB">$employee</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$type&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$reflection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getType</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!&nbsp;</span><span style="color: #0000BB">$type</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">isDataType</span><span style="color: #007700">())&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$type</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getProperties</span><span style="color: #007700">()&nbsp;as&nbsp;</span><span style="color: #0000BB">$property</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #0000BB">$property</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getName</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>    <div class="example-contents"><p>The above example will output:</p></div>    <div class="example-contents"><pre><div class="cdata"><pre>nameSNmanager</pre></div>    </pre></div>   </div>  </p> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="sdo.examples.html">Examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.sdo.html">SDO Functions</a></div> <div class="up"><a href="sdo.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?