myEngine

Russian

Introduction.
MyEngine is an example of use of technologies XML/XSLT with PHP. That it is better to understand that such of myEngine I shall do small retreat. So...

To bet, to work with PHP you began a like this:
<html>
  <head>
    <title><?php print $page_title; ?></title>
  </head>
  <body><?php showBodyContent(); ?></body>
</html>
i.e. inserted PHP code directly in HTML marking. And than poorly? So PHP began. Then, probably, you have begun to use Template Engines or even to write own. All this was required then, when the projects have increased, when for operation above the project were united some man, when began a division of labour of the designer and programmer. Then have begun to appear disadvantages of the above-stated approach: closely coupled interface of design and php code; sequential processing of the document. These and some other disadvantages have tried to correct with the help Template Engines. Thus to have to study used syntax and operation such Engines.

But here the technology XML/XSLT which has appeared has not these disadvantages on definition. How to apply it to the needs?
For the beginning, I want to pay your attention to some circumstances. What to you reminds the above-stated example of the code?.. The XML document! And to what the inserts PHP code are similar?.. On processing-instruction. So, we can use habitual to us the approach. You see the XML document does not contain design, and the sequence of processing to us is not important. As before, the PHP processor parses the file, will execute the code and will give back all in browser.
And if browser does not know how to process the XML document? Then it is possible to intercept the output of page (for example using buffering of the output) and to give back to the XSLT processor. In most cases it is enough of such approach.

But. As always there is any "But". The PHP processor perceives contents as string. Both earlier and now, that made yours PHP code, it should produce string which will be sent in browser. And all that up to your code in the beginning of page - code was output it is not known. Such functionality can appear unsufficiently. Here if PHP parse the XML document in Document Object Model also gave the interface to this document.

Imagine that you have such of PHP. Then you write ordinary the XML document and insert, where the PHP code is necessary. But now your code can return not only string but also objects - descendants of the object Node. If the string comes back, she is added in the DOM document as a text node. If the object - descendant of the object Node comes back, it is imported in the DOM document and is included in a tree. Then the DOM document is given back to the XSLT processor.

Just such approach allows to realize the myXML library (and with an exit PHP5 it will be possible and without this library). And the myEngine class is an example of implementation of the given approach.

Certainly there are also limitations. PHP processing-instruction can not be inserted into values of attributes. Therefore in myXML the support of variables and constants in values of attributes is included, and also execution of php code. For example:
<?xml version="1.0">
<?xml-stylesheet href="style.xsl" type="text/xsl">
<page title="<?php return PAGE_TITLE; ?>">
  <!-- So to make it is impossible. Use this -->
  <page title="{$page_title}">
  <!-- or -->
  <page title="{PAGE_TITLE}">
  <!-- or -->
  <page title="php: return $page_title;">
</page>
In the first case there should be a global variable $page_title which value and will be used as attribute. In the second case there should be a constant PAGE_TITLE. Otherwise value of attribute will be a name of a constant.
The following limitation is a visibility of variables. The PHP code is executed in environment of a method myXML::processingInstructionHandler(). At processing PHP processing-instruction in this method the simple code is executed:
function processingInstructionHandler($parser, $target, $data)
{
    ...code...
    
    $keys = array_keys($GLOBALS);
    foreach ($keys as $key) {
        $$key =& $GLOBALS[$key];
    }
    
    ...code...
}
which creates local variables - the references on global variables with the same name (you can ask why the extract() function is not used - because it creates copies of global variables, and only since version PHP 4.3 it knows how to make the references). In result, PHP processing-instruction are executed as though in uniform global environment. But if you declare a variable inside PHP processing-instruction it will belong to environment of a method myXML::processingInstructionHandler(). That she has become global take advantage of the array $GLOBALS or global statement. For example:
<?xml version="1.0">
<?xml-stylesheet href="style.xsl" type="text/xsl">
<?php $GLOBALS['simple_var'] = 'Simple String'; ?>
<!-- or -->
<?php
  global $simple_var;
  $simple_var = 'Simple String';
?>
<?php
  //Here it is not so necessary to declare a global variable.
  return $simple_var;
?>
</page>
And at last, in one unit processing-instruction it is possible to output only one value of string or object.

Minimum explanations on functions work brought immediately in code. Detailed explanations will be on claiming measure. On all of questions write on

Links.
ChangeLog.
Download.

On all of questions write on