It's sort of hard to explain in a single Stack Overflow question — if you want the full details in a less compressed way, No Frills Magento Layout walks through the functioning of the layout system in detail. (self link, book I wrote and sell, but I honestly don't know of a better resource)
High level though is sounds like you have two questions: How is the updates array populated, and why are the Simple XML objects a Varien_Simplexml
object.
Answering the second question first, when Magento instantiates its SimpleXML objects it uses the little known $class_name
parameter of simplexml_load_string
and simplexml_load_file
. This is a PHP feature which allows a user to specify what sort of objects should represent the SimpleXML nodes. Magento passes in the string Varien_Simplexml
, which means PHP tries to create each object returned by these functions as a Varien_Simplexml
object. Varien_Simplexml
extends the base SimpleXML
object, which means these XML objects behave exactly the same as a normal PHP SimpleXML
object except they also have the additional methods defined by Varien_Simplexml
.
As for loading the updates in "the right order", here's how that update array is populated. First, Magento loads all the layout XML files in a theme (page.xml
, catalog.xml
, etc; saving the local.xml
for last) into a single giant XML object called the package layout. Then, Magento goes through each layout "handle" (default
, cms_index_index
, etc) that's been specified by other parts of Magento, and looks for package XML layout nodes that match each handle name. As it finds each of these nodes, they're added to the update array.
So, to your titular question, order is influenced by two things. First, the order of the handles — all the default
handle nodes are loaded into the update array first, followed by STORE_default
, followed by the full action handle update (cms_index_index
), etc. Second, order is included why the way PHP returns child XML nodes. This is currently (and likely to remain) the order the nodes appear/were-added-to the package XML document.