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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:11:48+00:00 2026-06-16T19:11:48+00:00

I am trying to generate an xml file using the below class, when an

  • 0

I am trying to generate an xml file using the below class, when an array data is passed with no depth constraints the xml gets generated by the below code successfully. I am just trying to figure out as how self::createNode($value, $child); statements works does it create a new object? what is the significance of the the parent::_construct statement in the class constructor ? as the encoding is initialized already in the child class constructor.
What I have read since now about the self keyword is that it is used to call the static methods, but here the createNode method is non static. Will someone be able to help me out in understanding the context of this statement, what I can say here is the the DomDocument class doesn’t have any createNode method at all. Will anyone be able to help?
Thank you very much.

class array2xml extends DomDocument
{
    public $nodeName;
    private $xpath;
    private $root;
    private $node_name;
    public $xml_data;

    public function __construct($root='root', $node_name='node')
    {
        parent::__construct();
        $this->encoding = "UTF-8";
        $this->formatOutput = true;
        $this->node_name = $node_name;
        $this->root = $this->appendChild($this->createElement($root));
        $this->xpath = new DomXPath($this);
    } 

    public function createNode( $arr, $node = null)
    {   
        if (is_null($node))
        {
            $node = $this->root;
        }
        foreach($arr as $element => $value) 
        {
            $element = is_numeric( $element ) 
                ? $this->node_name 
                : $element;
            $element = htmlspecialchars($element,ENT_QUOTES,'UTF-8');
            $child = $this->createElement($element, (is_array($value) 
                ? null 
                : htmlspecialchars($value,ENT_QUOTES,'UTF-8')));
            $node->appendChild($child);
            if (is_array($value))
            {
                self::createNode($value, $child);
            }
        }
    }

    public function __toString()
    {
        $this->xml_data= $this->saveXML();
        return $this->saveXML();
    }
}
  • 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-06-16T19:11:50+00:00Added an answer on June 16, 2026 at 7:11 pm

    In PHP self always refers to the class where self was executed. So in your case self will refer to a method inside the class array2xml.

    Usually self is used to call static methods. Since in your case the method that is called by self isn’t static, it would have been better to use $this instead.

    Where parent always calls the method from the “parent” class. So the class that is extended. Again in your case when parent::some function() is called, it will search for that method in the DomDocument class. Because that is the “parent” (extended) class.

    The reason that parent::__construct() is called in the constructor of the array2xml class, is because the constructor of the extended class is never called automatically when your own class also uses a __construct(). Unless your class does not have a __construct(). Only then PHP will call the __construct() of the parent class. Otherwise you’ll have to call it manually from your own constructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to generate an XML file using the data from a class,
I'm trying to use append to generate html using data from an xml file.
I am trying to generate xml file as a background task using resque gem
I am trying to generate an xml file of a recipe database that has
I'm trying to generate a RDoc using the XML format. Here's the command that
I'm trying to generate svg-Code in a web-application. Here's an example output: <?xml version=1.0
I'm trying to generate a client using wsdl2java. The wsdl is as follows: <?xml
I have parametrized persistence.xml. I am trying to generate ddl schema using hbm2ddl. How
I am using xsd to generate classes from an xml file. It is working
I'm trying to generate an XML file with the my machine's hostname in some

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.