Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7440413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:50:51+00:00 2026-05-29T10:50:51+00:00

I have created a class extending SimpleXMLElement which loads an XML containing translations for

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:50:52+00:00Added an answer on May 29, 2026 at 10:50 am

    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]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a CloseableBlockingQueue extending ArrayBlockingQueue: private static class CloseableBlockingQueue<E> extends ArrayBlockingQueue<E> {
i have created a My custom listActivty by extending ListActivity like this. public class
I have created a class for a dashboard item which will hold information such
I have created a class library called AddServiceLibrary in which I have a method
I am using C#. I have created a class which can be included in
I am using the apache library. I have created a class which sends a
I have created a class call DatabaseHandler extending SQLiteOpenHelper. I want to explicting deleted
I have created a my own ContentProvider by extending class ContentProvider with all its
I have created an abstract class which extends the Activity Class. I want to
I have created a class that is simply THIS Class UserControlBase Inherits UserControl End

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.