Reflecting on Service Data Objects
SDOs have a knowledge of the structure they have been created to represent (the model). For example, a Company SDO created using the Company XML schema above would only be permitted to contain DepartmentType data objects which in turn could only contain EmployeeType data objects.
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.
Example #1 Reflecting on a Data Object
The following example shows how we can reflect on an empty Employee data object.
<?php
// Create the employee data object (e.g. from an XML Data Access Service)
$employee = ...;
$reflection = new SDO_Model_ReflectionDataObject($employee);
print($reflection);
?>
The above example will output:
object(SDO_Model_ReflectionDataObject)#4 { - ROOT OBJECT - Type { companyNS:EmployeeType[3] { commonj.sdo:String $name; commonj.sdo:String $SN; commonj.sdo:Boolean $manager; } }
Using print on the SDO_Model_ReflectionDataObject writes out the data object'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).
Example #2 Accessing the type information
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).
<?php
// Create the employee data object (e.g. from an XML Data Access Service)
$employee = ...;
$reflection = new SDO_Model_ReflectionDataObject($employee);
$type = $reflection->getType();
if (! $type->isDataType()) {
foreach ($type->getProperties() as $property) {
print $property->getName() . "\n";
}
}
?>
The above example will output:
name SN manager
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-sdo.sample.reflection.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.