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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:27:28+00:00 2026-05-23T12:27:28+00:00

I’m writing a program that reads a series of tasks from an XML file

  • 0

I’m writing a program that reads a series of tasks from an XML file and puts them within dynamically generated “task” objects (e.g., task1, task2, task3, task4 etc.).
Each taskXX object has properties (task1->name, for example) that are populated by data in the XML file.

My program runs through the XML file and generates a bunch of objects filled with data. However, I can’t use those objects in the rest of my program.

Why is this the case? I’m pretty sure this has to do with scope but I can’t seem to figure it out. Also, I’m not keen on using the global keyword.

Here’s some of the code:

// -- CODE INSIDE FILE: class_todos.php -- //
class readTodos {
    public function loadAll() {
        $counter = 0;

        $xml = simplexml_load_file("todos.xml");

        foreach($xml->task as $task) {
            ${'task'.$counter} = new Task;
            $theTask = ${'task'.$counter};

            $theTask -> set('id',   $task['id']);
            $theTask -> set('name', $task->name);
            $theTask -> set('note', $task->note);
            // and so on and so on...

            counter++;
        }
    }
}

// -- CODE INSIDE FILE: class_task.php -- //
class Task {
    private $id;
    private $name;
    private $note;
    // etc. etc.

    public function set($varname,$value) {
        $this->$varname = $value;
    }
    public function get($varname) {
        return $this->$value;
    }
}

// -- CODE INSIDE FILE: index.php -- //
require_once('class_todos.php');

<ul class="todo-list">
    <?php
        echo $task0 -> get('name');
        // The above statement give the error statement: "Undefined variable: task0"
        // I know for sure that $task0 IS defined within the scope of the class "readTodos".
        // How do I make the $task0 (and upwards... e.g., task1, task2, task3, etc.)
        // available for use in the rest of my program?
    ?>
</ul>
  • 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-23T12:27:29+00:00Added an answer on May 23, 2026 at 12:27 pm

    You can’t use classes that way. The loadAll method should return an array of tasks instead of creating variables. When it creates variables inside the method, it’s scope is limited to that method as therefore, it’s not available outside it. You should also consider making that function static.

    Instead, use something like this:

    class TodoManager {
        public static function loadAll() {
            $counter = 0;
    
            $xml = simplexml_load_file("todos.xml");
            $allTasks = array();
    
            foreach($xml->task as $task) {
                $theTask = new Task;
                $theTask->set('id',   $task['id']);
                $theTask->set('name', $task->name);
                $theTask->set('note', $task->note);
                // and so on and so on...
    
                // Add this todo to the array.
                $allTasks[] = $theTask;
            }
    
            return $allTasks;
        }
    }
    

    Your HTML should then do:

    $todos = TodoManager::loadAll(); // Note, I've renamed your class to TodoManager.
    
    // Loop through the todos.
    foreach ( $todos as $todo )
    {
      echo $todo->get('name');
    
      // Do more useful stuff.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.