PHP OOP在create_function中调用私有函数作为回调

I have created a class extending SimpleXMLElement which loads an XML containing translations for a multilingual website. In this class are two private functions - get and translate. The former returns the translation contained in the node accessed by the xpath passed as an argument.

The latter replaces tag-like substrings in a string (eg. "Lorem ipsum #{dolor} sit amet, consectetur adipiscing elit.") with their translation obtained via the former function - the tag being a sort of xpath I process in my get() function.

I'm using preg_replace_callback in translate() - for I cannot pass a back reference as an argument to a function in preg_replace - to send matched occurrences to get(), which will replace them with the translation.

Here is a shortened version of my class :

$translation = new Translation('path_to_my_xml', null, true);

class Translation extends SimpleXMLElement {
    function get($xpath){
        // xpath is of the form 'parent_node/child_node'
        // After some processing and the wanted node being found - it returns the translation
    }

    function translate($string){
        $string = preg_replace_callback('/#\{([a-z0-9-\/]+)\}/', create_function('$matches', 'return $this->get($matches[1]);'), $string);
    }
}

Of course, I get a fatal error : using $this when not in object context, for the class hasn't been instantiated in my create_function call - I tried self::get($matches[1]) as well without success.
These functions cannot be public for my class being an extension of SimpleXMLElement, it requires the path to an XML in the constructor. So I can't do this : create_function('$matches', 'return Translation::get($matches[1]);').

I hope I made myself clear. The only workaround I see is passing the path to my XML in my translate() function and make it public, but this would be very inconvenient. Do you see any other way out ?

Thanks

Hang on, can't you just use an old style array PHP callback like this?

function translate($string){
    $string = preg_replace_callback('/#\{([a-z0-9-\/]+)\}/', 
                                    array($this, 'translateCallback'), 
                                    $string);
}

public function translateCallback($matches)
{
  return return $this->get($matches[1]);
}

The facility to use $this in lambda/anonymous functions was added in PHP 5.4, suspect this is similar; but I'd have to run up my testbed to check.

You could try:

$string = preg_replace_callback('/#\{([a-z0-9-\/]+)\}/', function($matches) use ($this) { return $this->get($matches[1]); }, $string); 

Probably not terribly helpful, but in PHP 5.4 anonymous functions can access $this. When's your ship date?

1:

preg_replace_callback($regexp, array($this, 'someMethod'), $string)

2:

$myFunc = function($matches) use ($this) ...
...
preg_replace_callback('/#\{([a-z0-9-\/]+)\}/', $myFunc, $string)

3: Override __construct in Translation to save last instance, implement Translation::getLastInstance() and use it inside create_function or implement Translation::get($m) that use last instance and pass it to preg_replace_callback as array('Translation', 'get'), or just save before preg_replace_callback link to current instance to self::$currentInstance. In any case the sence is in saving a link to the instance into static property of Translation