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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:56:12+00:00 2026-05-20T01:56:12+00:00

inside a computer….Ok, here is my question. I’m creating a PHP object that will

  • 0

inside a computer….Ok, here is my question.

I’m creating a PHP object that will echo out a XML document. I’m putting in a date/time stamp as a default. I’m starting out using the constructor to generate the time stamp.

The roadblock I’ve hit is how to to use different methods to access the XML document that was created inside the constructor. I’m trying to expand my knowledge about OOP so a nudge in the right direction would be appreciated.

<?php //XML DOM OBJECT CREATOR 9000

// Create new DOM object
$dom = new Xmlstuff;
$dom->generateError('This is the error');
$dom->addtime();
$dom->generateXML();

    class Xmlstuff extends DOMDocument{

    //Constructor
        public function __construct(){
        //Calling constructor of DOMDocument
        parent::__construct('1.0','utf-8'); 

        } //End of constructor


        function generateError($errorMsg){

            //Generate standard response 
            //Root Node
            $rootNode= $this->createElement('root','');
            $this->appendChild($rootNode);

            //status Node
            $statusNode=$this->createElement('status',' '); 
            $rootNode->appendChild($statusNode);

            //Error Message
            $errorElement=$this->createElement('error' ,$errorMsg); 
            $statusNode->appendChild($errorElement);

            //date
            $dateElement=$this->createElement('date', date("d/m/Y"));
            $statusNode->appendChild($dateElement);

            //time
            //$timeElement=$this->createElement('time', date("H:i:s").' PST');
            //$statusNode->appendChild($timeElement);
            } 


            function addtime(){
            //time
            $timeElement=$this->createElement('time', date("H:i:s").' PST');
            $statusNode->appendChild($timeElement);
            }


            //Function to display generated XML document
            function generateXML(){
            header('Content-Type: text/xml');
            echo $this->saveXML();  
            }

} //End of Class


?>
  • 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-20T01:56:13+00:00Added an answer on May 20, 2026 at 1:56 am

    You are extending the DOMDocument, so anything on DOMDocument you want to access is accessible through $this/self Xmlstuff “is a” DOMDocument

    Xmlstuff is an extension of DOMDocument. So anything behaviour or data that DOMDocument contains, your new class Xmlstuff also contains.

    The call to parent::__construct(); is just saying, after I’ve done specific initialization for the Xmlstuff class, do all the initialization needed for the DOMDocument.

    Any data/functions declared in the DOMDocument class as public or protected will be inherited by your Xmlstuff class.

    Looking further at your code, the addtime function doesn’t have access to $statusNode, if you want access to it (the $statusNode created in generateError), then you need to make it a member variable. $this->statusNode.

    function addtime(){
        //time
        $timeElement=$this->createElement('time', date("H:i:s").' PST');
        $this->statusNode->appendChild($timeElement);
    }
    

    Note: you will have to create it as well in the generateError function.

    Additionally to make you code tidier, you should initialize $this->statusNode in your constructor. The reason being a class should hide its implementation details (it should be a black box to anyone that wants to use it). If someone called the function addTime() before calling generateError() then $this->statusNode will not have been created yet.

    i.e. Add this line to your constructor:

    $this->statusNode = $this->createElement('status', ' ');
    

    replace this line from generateError()

    $statusNode=$this->createElement('status',' '); 
    

    with

    $statusNode = $this->statusNode;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.