Subject: XSLT php:function: call objects non static method
Posted By: polarbear Post Date: 4/19/2008 1:17:43 PM
Hi,

as you all know it is possible to call php functions from inside a xslt template like this:

<xsl:value-of select="php:function('strtoupper', string(.) )" />
<xsl:value-of select="php:function('aClass::aStaticFunction', string(.))"/>

(Assumed registerPhpFunctions() is called and the php namespace is set)

My code uses the singleton pattern and therefore there is one instance of a certain object. The php application invokes its methods like this:
$GLOBALS['aSingleton']->aNonStaticFunction($param);


Question:
How can I call this method from inside the XSLT? Obviously this won't work:
<xsl:value-of select="php:function('$GLOBALS['aSingleton']->aNonStaticFunction', string(.) )" />


Thanks in advance
Best regards

Reply By: philip_cole Reply Date: 6/6/2008 5:39:37 PM
Hi,

I haven't done much with PHP XSL, but you can actually pass other arguments to the function other than the xsl bit.
The real problem with your code is that you can't pass arrays to the XSL functions, and the quotes are a right pain.

For the most part, you should be able to get round this by creating a simple global wrapper function which can call any other one. This can take the name of the global variable, the function name, and the argument[s]. At its most basic:

function CallGlobalFunction($objName, $funcName, $arg)
{
  return $GLOBALS[$objName]->$funcName($arg);
}

Your XSL can then call this when it needs an instance:

<xsl:value-of select="php:function('CallGlobalFunction', 'aSingleton', 'aNonStaticFunction', string(.))" />


Hope this Helps
Phil

Go to topic 71932

Return to index page 1