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

  • Home
  • SEARCH
  • 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 6173907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:39:24+00:00 2026-05-23T23:39:24+00:00

Im new to PHP and would like to get some advice on the following

  • 0

Im new to PHP and would like to get some advice on the following situation:

I have a file in which Im parse an XML file. My strategy has been create a “parser.php” file. Then create separate files for each tag in the XML file. Each of these separate files is a class that when instantiated will store all the attributes of that particular tag.

I have several loops in the parser that instantiate the class that corresponds with the tag when they encounter it.Once the loop is done that object is then inserted into a global array.

Here is the code:

<?php
include ('class.Food.php');
include ('class.Drink.php');
$xml = simplexml_load_file("serializedData.xml");

//Global Variables==============================================================

$FoodArray = array();
$DrinkArray = array();
$FoodObj;
$DrinkObj;

foreach($xml->children() as $child)
{
//Instantiate a Food object here and insert into the array
$FoodObj = new Food();
//Add attributes of tag to the Food object
array_push($FoodArray, $FoodObj);

}

Thus at the end of each loop there would be a food object created and that would be added to the FoodArray.

My questions are:

  1. Do I have to explicitly call a destructor or free the object memory at the end of the loop?
  2. Using the above syntax, will the FoodObj be stored in the FoodArray as a reference or a value
  3. Each time the variable Food gets a different instance stored in it, does that matter when stored in the array? All the objects stored in the array go by the index number right?

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-23T23:39:24+00:00Added an answer on May 23, 2026 at 11:39 pm

    Try the following

    $FoodArray = array();
    $DrinkArray = array();
    
    foreach($xml->children() as $child) 
    {
        //Instantiate a Food object here and insert into the array 
        $FoodObj = new Food(); 
        //Add attributes of tag to the Food object 
        $FoodArray[] = $FoodObj;
        unset($FoodObj);
    }
    

    1) unset’ng the instance manually is good form, but unnecessary. the php garbage collector will do it for you anyways and is very efficient.

    2) in the example above, $FoodArray[i] will continue to point to the instance even after $FoodObj is unset.

    3) it works whether $FoodObj is local to the loop or not. But for best practice it should be.

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

Sidebar

Related Questions

I would like to redirect as such... http://old.com/a/b/ -> http://new.com/y/z/ http://old.com/a/b/file.php -> http://new.com/y/z/ http://old.com/a/b/c/file.php
I am new to php. And I would like to know some of the
I'm trying to get some data from a PHP file which only gives a
I am relatively new to mod_rewrite, but have a site which I would like
I am very new to php and simplepie. I would like to be able
PHP/MySQL (CodeIgniter) I would like to add new interest_keywords in the exist database value.
In PHP, to create a new object you would do something like this, $dog
I've got some PHP cruft that I would like to delegate methods. Sort of
I'm doing some PHP memory benchmarks and i would like to obtain the garbage
I would like to know which of the following methods would be best to

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.