PHP callback from XSLTScope of ChangeEnable callbacks to PHP functions from within XSL through the php:function() callback supplied by PHP 5. Rationale
Sometimes complex functionality can be (more) easily calculated with
PHP rather than with XSL. Even more, sometimes results depend on huge
amount of information that would need to be transferred into XML to
be available in the XSL transformation. FunctionalityThe DomXSLProcessor will have a method registerInstance() where a PHP object instance can be registered under a defined name: $myContext= new WebsiteContext();
From within XSL one can call the an invoke method which takes
define name, method name and method arguments and invokes the
given method in a non-static context: <?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:php="http://php.net/xsl"
>
<xsl:template match="/">
<xsl:value-of select="php:function(
'XSLCallback::invoke',
'context',
'hasPermission',
'view'
)"/>
</xsl:template>
</xsl:stylesheet>
This invokation translates into the PHP function call on a method hasPermission() with the
argument "view" on the object formerly registered as "context". The method must have
the annotation @xslmethod: class WebsiteContext extends Object {
Security considerations
As every callable method must be explicitely annotated fine grained control
over methods that can be called is achieved and thus good security. Speed impact
When not using the callback, there's no impact. When using it, there's a negative
impact through the context switch from XSL to PHP which is believed to be more
than equalized by the advantage of faster calculations in PHP and the removal of
lots of XML nodes in the tree. Dependencies
n/a Related documents
- http://xp-framework.net/rfc/contrib/rfc0104.diff Comments
- kiesel, Thu Dec 28 19:52:56 CET 2006
We could also just allow every PHP method to be called, simplifying this
callback issue. But, then we have no control over what is called. | Table of contents |