Very naïve code might do something like this:
function render_some_info($imageid) {
$image = ImageFactory::get($imageid);
echo '<div class="container">';
echo '<img src="',$image->get_src(),'">';
echo '</div>';
}
You get the data about the image in the ImageFactory call and output it on the spot. In a better MVC architecture, that factory call would be in a controller. But for the sake of the argument, lets imagine that you couldn't predict that $imageid early enough, it came from another data fetching/logic.What we would do with datex would look like that:
function render_some_info($imageid) {
$datex = new_datex();
echo '<div class="container">';
$datex->merge(new ImageDatum($imageid));
echo '</div>';
return $datex->end();
}
The datex object captures the DOM output, and the datum object, keeping the order. The datum object is just a shell, it contains only the $imageid at this point, nothing else.The $datex object returned by this method, is then merged into the general output tree that represents your page.
Processing continues until you've built your entire page. You still haven't touched the DB or cache for anything about that $imageid.
Then the resolving phase happens, you traverse your $datex tree object, that contains your entire page, and you look for all objects of type ImageDatum. Say you have 10 of them, you get the $imageid they contain, remove the dupes, then you do your efficient one-time DB query that gets the URL data for all these images at once. You iterate through all the ImageDatum objects and give them the data they need.
Once you've ran out of datum objects that needed data, you know that they're all resolvable (populated). You run through the $datex tree once more, you echo all the DOM you find, and you call ->render() on all the objects you find. In the case of ImageDatum your render() method will look like:
function render() {
echo '<img src="',$this->src,'">';
}
Where the $src member was populated by the resolving phase of datex (right after hitting the DB once).Does that give you a better idea, or do I need to go into more details about this specific example?