p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Pro PHP (http://p2p.wrox.com/forumdisplay.php?f=96)
-   -   XSLT php:function: call objects non static method (http://p2p.wrox.com/showthread.php?t=67544)

polarbear April 19th, 2008 01:17 PM

XSLT php:function: call objects non static method
 
Hi,

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

Code:

<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:
Code:

$GLOBALS['aSingleton']->aNonStaticFunction($param);
Question:
How can I call this method from inside the XSLT? Obviously this won't work:
Code:

<xsl:value-of select="php:function('$GLOBALS['aSingleton']->aNonStaticFunction', string(.) )" />
Thanks in advance
Best regards


philip_cole June 6th, 2008 05:39 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


All times are GMT -4. The time now is 12:23 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.