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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:52:59+00:00 2026-06-15T09:52:59+00:00

I have a very weird problem that when I call the method of a

  • 0

I have a very weird problem that when I call the method of a class from within the constructor I get as a result empty arrays, but when I call the method from the class instance it works fine.

Here is the code for loadxml.php:

include('XMLNode.php'); // this is a simple object class

class LoadXML
{
public $MyNodes = array();

public function __constructor($xmlFile)
{
    $this->getNodes($xmlFile);
}

public function getNodes($xmlFile)
{
    if (file_exists($xmlFile)) {
        $xmlNodes = simplexml_load_file($xmlFile);

        foreach ($xmlNodes->XMLNode as $key => $value) {
            array_push($this->MyNodes, $value);
        }
    }
    else
    {
        exit('Failed to open xml file '.$xmlFile);
    }
}
}

Now in the main file if I make the call like this:

include('loadxml.php');

$nodes = new LoadXML('myXMLFile.xml');

print "<pre>";
print_r($nodes);
print "</pre>";

… the result looks like this:

LoadXML Object
(
[MyNodes] => Array
(
)
)

If I make the call from the class instance like this and comment out the call to getNodes from the LoadXML constructor:

include('loadxml.php');

$nodes = new LoadXML();
$nodes->getNodes('myXMLFile.xml');

print "<pre>";
print_r($nodes);
print "</pre>";

I will get the correct output. What’s happening? What am I doing wrong in the first approach? Am I missing something from the class constructor? I looked in the manual (http://php.net/manual/en/oop4.newref.php) and the call to the method seems legit from within the constructor.

  • 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-15T09:52:59+00:00Added an answer on June 15, 2026 at 9:52 am

    Try renaming your constructor to __construct

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

Sidebar

Related Questions

it's a weird problem that I have I have a very simple constructor that's
I have a very weird problem that I can't understand. This is C code:
I have a very weird problem.That my web project deployed to the Weblogic 10.0,Monday
I have a very weird problem. I have a VB.NET 2.0 application that takes
I have encountered a very weird problem with fancybox modal that whenever my website
I have a very weird problem: sometimes when I call nHibernate update to an
I have a problem that is very confusing for me. In the constructor of
I have very weird issue. I have method that called by wcf that hosted
I have a very weird problem.. I really do hope someone has an answer
I have a very weird problem in Firefox ( version 3.5.2), and I am

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.