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 880737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:08:51+00:00 2026-05-15T12:08:51+00:00

So I have a PHP file that I use to define all my constants

  • 0

So I have a PHP file that I use to define all my constants but now want the flexibility of using a XML instead.

Example PHP Config file

define("LOGIN_URL", "secure_login.php");
define("REDIRECT", "redirect.php");

define("MAPPED_VALUE_GREEN", "object_green");
define("MAPPED_VALUE_KEY", "object_key_12345");

What I’m going to do is:

<scriptNameConfig>
  <urls>
     <url alias="LOGIN_URL" scriptname="secure_login.php" location="optional/path/to/file"/>
     <url alias="REDIRECTL" scriptname="redirect.php" location="optional/path/to/file"/>
  </urls>
  <mapping>
     <mapped name="MAPPED_VALUE_GREEN" data="object_green"/>
     <mapped name="MAPPED_VALUE_KEY" data="object_key_12345"/>
  </mapping>
</scriptNameConfig>

Now this is okay but I want to use XPATH to extract the values from this type of config file but would like it to be dynamic enough so I don’t have to hard code the functions

This is my base class

class ParseXMLConfig {
    protected $xml;

    public function __construct($xml) {
        if(is_file($xml)) {
            $this->xml = simplexml_load_file($xml);
        } else {
            $this->xml = simplexml_load_string($xml);
        }
    }
}

And I extend it like this

class ParseURLS extends ParseXMLConfig {

public function getUrlArr($url_alias) {
        $attr = false;
        $el = $this->xml->xpath("//url[@alias='$url_alias']");
        if($el && count($el) === 1) {
            $attr = (array) $el[0]->attributes();
            $attr = $attr['@attributes'];
        } 
        return $attr; // this will return the element array with all attributes
    }
}

But the problem is if I wanted to introduce a new value in the confie file I have to add some sort of function for XPATH to parse it. wanted to know if anyone has an idea on how to go about doing this generic/dynamic so adding/changing or removing elements/attributes in the XML config file would be easier and less hard coding.

Thanks for any tips/examples/code/ideas,
-Phill

  • 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-15T12:08:51+00:00Added an answer on May 15, 2026 at 12:08 pm

    I’m curious – exactly what flexibility do you think you’re getting from this? You’re going through an awful lot of work to put stuff in XML just to convert it to PHP data.

    If you’re ok with ditching XML (i.e., nothing else is using this configuration), why not something like this?

    class Config
    {
      private static $urls = array(
          'LOGIN_URL' => array(
              'scriptname' => 'secure_login.php'
            , 'location'   => 'optional/path/to/file'
          )
        , 'REDIRECTL' => array(
              'scriptname' => 'redirect.php'
            , 'location'   => 'optional/path/to/file'
          )
      );
    
      public static function getUrlArr( $alias )
      {
        if ( isset( self::$urls[$alias] ) )
        {
          return self::$urls[$alias];
        }
        throw new Exception( "URL alias '$alias' not defined" );
      }
    }
    

    Also, as a side note, I have some reservations about your choice to put a verb (parse) in a class name. If you’re gonna go that route, try using a noun-representation of that verb.

    class XmlConfigParser {}
    

    EDIT

    In that case, then you can’t have any method names that refer to the XML node structure (unless you want to get into the world of meta programming)

    Really no matter what you do, you’re just going to be creating syntactic sugar over an XPath expression

    Here are examples of methods you might make

    // Use XPath from the client
    $config->fetchAttributesByXpath( "/urls/url[@alias='LOGIN_URL']" );
    
    // Abstract away the XPah
    $config->fetchAttributes( 'urls.url', 'alias', 'LOGIN_URL' );
    

    For example, symfony does something like this with their config files, which are YAML. In a config file like such

    // app.yml
    urls:
      LOGIN_URL: {scriptname: 'secure_login.php', location='optional/path/to/file'}
    

    You would retrieve the value like so

    $values = sfConfig::get( 'app_urls_LOGIN_URL' );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP client that requests an XML file over HTTP (i.e. loads
Alright, currently I have my SWF hitting a php file that will go and
I have a PHP script that processes file uploads. The script tries to organise
I have an htaccess file that uses mod_rewrite to redirect /controller to /index.php?controller=%controller% Like
I have a PHP script that pushes the headers to allow a file to
I have some jQuery code. I have called an Ajax function file, file.php, that
I have a php file which I will be using as exclusively as an
i have a php file launching my exe. the exe does cout and the
I have a PHP file, Test.php, and it has two functions: <?php echo displayInfo();
I have a PHP file and an image in the same directory. How could

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.