📄 sdo.sample.getset.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Setting and Getting Property Values</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="sdo.sample.sequence.html">Working with Sequenced Data Objects</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.getset" class="section"> <h2 class="title">Setting and Getting Property Values</h2> <p class="para"> The following examples assume <strong class="command">$company</strong> is the root of a tree of data objects created from the schema and instance document shown above. </p> <p class="para"> <div class="example"> <p><b>Example #1 Access via property name</b></p> <div class="example-contents"><p> Data object properties can be accessed using the object property access syntax. The following sets the company name to 'Acme'. </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$company</span><span style="color: #007700">-></span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #DD0000">'Acme'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #2 Access via property name as array index</b></p> <div class="example-contents"><p>We can also access properties using associative array syntax. The simplest form of this uses the property name as the array index. For example, the following sets the company name and gets the employeeOfTheMonth. </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$company</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">] = </span><span style="color: #DD0000">'UltraCorp'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$eotm </span><span style="color: #007700">= </span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">'employeeOfTheMonth'</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #3 Data Object iteration</b></p> <div class="example-contents"><p> We can iterate over the properties of a data object using foreach. The following iterates over the properties of the employee of the month. </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /> $eotm </span><span style="color: #007700">= </span><span style="color: #0000BB">$company</span><span style="color: #007700">-></span><span style="color: #0000BB">employeeOfTheMonth</span><span style="color: #007700">;<br /> foreach (</span><span style="color: #0000BB">$eotm </span><span style="color: #007700">as </span><span style="color: #0000BB">$name </span><span style="color: #007700">=> </span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"$name: $value\n"</span><span style="color: #007700">;<br /> }<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p> which will output: </p></div> <div class="example-contents"><div class="cdata"><pre>name: Jane DoeSN: E0003</pre></div> </div> <div class="example-contents"><p> The 'manager' property is not output, because it has not been set. </p></div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #4 Access many-valued property by name</b></p> <div class="example-contents"><p> Many-valued data object properties can also be accessed using the object property name syntax. The following gets the list of departments. </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$departments </span><span style="color: #007700">= </span><span style="color: #0000BB">$company</span><span style="color: #007700">-></span><span style="color: #0000BB">departments</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #5 Many-valued element access</b></p> <div class="example-contents"><p> We can access individual elements of many-valued properties using array syntax. The following accesses the first department in the company. </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$ad_tech_dept </span><span style="color: #007700">= </span><span style="color: #0000BB">$company</span><span style="color: #007700">-></span><span style="color: #0000BB">departments</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #6 Many-valued property iteration</b></p> <div class="example-contents"><p> Many-valued properties can also be iterated over using foreach. The following iterates over the company's departments. </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /> </span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$company</span><span style="color: #007700">-></span><span style="color: #0000BB">departments </span><span style="color: #007700">as </span><span style="color: #0000BB">$department</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">// ...<br /> </span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p> Each iteration will assign the next department in the list to the variable <strong class="command">$department</strong>. </p></div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #7 Chained property access</b></p> <div class="example-contents"><p> We can chain property references on a single line. The following sets and gets the name of the first department. </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /> $company</span><span style="color: #007700">-></span><span style="color: #0000BB">departments</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-></span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #DD0000">'Emerging Technologies'</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$dept_name </span><span style="color: #007700">= </span><span style="color: #0000BB">$company</span><span style="color: #007700">-></span><span style="color: #0000BB">departments</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-></span><span style="color: #0000BB">name</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p>Using the associative array syntax, this is equivalent to</p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /> $company</span><span style="color: #007700">[</span><span style="color: #DD0000">'departments'</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #DD0000">'name'</span><span style="color: #007700">] = </span><span style="color: #DD0000">'Emerging Technologies'</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$dept_name </span><span style="color: #007700">= </span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">'departments'</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #DD0000">'name'</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p> In either case, the dept_name variable is set to 'Emerging Technologies'. </p></div> </div> </p> <p class="para">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -