<!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>Setting and Getting Property Values</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="sdo.examples-basic.html">Basic usage</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" id="example-5443"> <p><strong>Example #1 Access via property name</strong></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" id="example-5444"> <p><strong>Example #2 Access via property name as array index</strong></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" id="example-5445"> <p><strong>Example #3 Data Object iteration</strong></p> <div class="example-contents"><p> We can iterate over the properties of a data object using <a href="control-structures.foreach.html" class="link">foreach</a>. 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">"</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">: </span><span style="color: #0000BB">$value</span><span style="color: #DD0000">\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 Doe SN: 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" id="example-5446"> <p><strong>Example #4 Access many-valued property by name</strong></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" id="example-5447"> <p><strong>Example #5 Many-valued element access</strong></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" id="example-5448"> <p><strong>Example #6 Many-valued property iteration</strong></p> <div class="example-contents"><p> Many-valued properties can also be iterated over using <a href="control-structures.foreach.html" class="link">foreach</a>. 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" id="example-5449"> <p><strong>Example #7 Chained property access</strong></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"> <div class="example" id="example-5450"> <p><strong>Example #8 XPath navigation</strong></p> <div class="example-contents"><p> The associative array index can be an XPath-like expression. Valid expressions are defined by an augmented sub-set of XPath. </p></div> <div class="example-contents"><p> Two forms of indexing into many-valued properties are supported. The first is the standard XPath array syntax with the indexing starting at one, the second is an SDO extension to XPath with an index starting at zero. The standard syntax is: </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /> $jane_doe </span><span style="color: #007700">= </span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">"departments[1]/employees[2]"</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>and the SDO XPath extension syntax is:</p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /> $jane_doe </span><span style="color: #007700">= </span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">"departments.0/employees.1"</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p> Both these examples get the second employee from the first department. </p></div> </div> </p> <p class="para"> <div class="example" id="example-5451"> <p><strong>Example #9 XPath querying</strong></p> <div class="example-contents"><p> We can use XPath to query and identify parts of a data object based on instance data. The following retrieves the manager from the 'Advanced Technologies' department. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /> $ad_tech_mgr </span><span style="color: #007700">= <br /> </span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">"departments[name='Advanced Technologies']/employees[manager=true]"</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5452"> <p><strong>Example #10 Creating child data objects</strong></p> <div class="example-contents"><p> A data object can be a factory for its child data objects. A child data object is automatically part of the data graph. The following add a new employee to the 'Advanced Technologies' department. </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: #DD0000">"departments[name='Advanced Technologies']"</span><span style="color: #007700">];<br /> </span><span style="color: #0000BB">$new_hire </span><span style="color: #007700">= </span><span style="color: #0000BB">$ad_tech_dept</span><span style="color: #007700">-></span><span style="color: #0000BB">createDataObject</span><span style="color: #007700">(</span><span style="color: #DD0000">'employees'</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$new_hire</span><span style="color: #007700">-></span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #DD0000">'John Johnson'</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$new_hire</span><span style="color: #007700">-></span><span style="color: #0000BB">SN </span><span style="color: #007700">= </span><span style="color: #DD0000">'E0005'</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$new_hire</span><span style="color: #007700">-></span><span style="color: #0000BB">manager </span><span style="color: #007700">= </span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5453"> <p><strong>Example #11 Unset a primitive property</strong></p> <div class="example-contents"><p> We can use the <span class="function"><a href="function.isset.html" class="function">isset()</a></span> and <span class="function"><a href="function.unset.html" class="function">unset()</a></span> functions to test and remove items from the data object. </p></div> <div class="example-contents"><p> The following clears 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 /> </span><span style="color: #007700">unset(</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> </p> <p class="para"> <div class="example" id="example-5454"> <p><strong>Example #12 Unset a data object</strong></p> <div class="example-contents"><p> unset can also be used to remove a data object from the tree. The following example shows John Jones leaving the company. </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">unset(</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">employees</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" id="example-5455"> <p><strong>Example #13 Unset a referenced data object</strong></p> <div class="example-contents"><p> The following removes the 'employeeOfTheMonth' from the company. If this were a containment relationship then the employee would be removed from the company (probably not a good idea to sack your best employee each month!), but since this is a non-containment reference, the employee being referenced will remain in the department in the company, but will no longer be accessible via the employeeOfTheMonth property. </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">if (isset(</span><span style="color: #0000BB">$company</span><span style="color: #007700">-></span><span style="color: #0000BB">employeeOfTheMonth</span><span style="color: #007700">)) {<br /> unset(</span><span style="color: #0000BB">$company</span><span style="color: #007700">-></span><span style="color: #0000BB">employeeOfTheMonth</span><span style="color: #007700">);<br /> }<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5456"> <p><strong>Example #14 Access via property index</strong></p> <div class="example-contents"><p> Data object properties can be accessed via their property index using array syntax. The property index is the position at which the property's definition appears in the model (in this case the xml schema). We can see from the schema listing above that the company name attribute is the second company property (the SDO interface makes no distinction between XML attributes and elements). The following sets the company name to 'Acme', with the same result as <a href="sdo.sample.getset.html#sdo.examples.propname" class="link">Access via property name</a> </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">1</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 class="example-contents"><p> Using the index directly in this way is likely to be fragile. Normally the property name syntax should be preferred, but the property index may be required in special cases. </p></div> </div> </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="sdo.examples-basic.html">Basic usage</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></body></html>