Jump to content

[SOLVED] Having Trouble With SimpleXML


scheda

Recommended Posts

Hey all,

 

I'm working on a class that uses SimpleXML to pull data from XML files to display to the user.

 

Here's the bit of code I'm running my head against at the moment.

<?php
class retrieveStats {
	//set XML file
	public $xml_stats = "./visits.xml";

	//load XML file
	$xml = simplexml_load_file($xml_stats);

	public function show_ids() {
		if (count($xml -> hit) > 0) {
			foreach ($xml -> hit as $a) {
				echo $a -> id;
			}//end foreach
		}//end if
	} //end show_ids
}//end class
?>

 

Right now I'm just trying to access to XML file so I can display anything, which is what the show_ids function is supposed to do.

 

Here is the code that's running the files in the class.

 

<?php
require_once("../includes/retrieve-stats.php");
$stats = new retrieveStats;
$stats -> load_xml();
?>

 

 

When running anything in this class, I get the following error.

 

Parse error: parse error, expecting `T_FUNCTION' in C:\wamp\www\revised-statistics\includes\retrieve-stats.php on line 7

 

And I've gotta say, this error is painfully vague.

 

Any ideas that you might have about resolving this issue would be fantastic.

 

Thanks a ton!

Link to comment
Share on other sites

try this

<?php
   class retrieveStats {
      //set XML file
      public $xml_stats = "./visits.xml";
      private $xml;
      function __construct()
      {
      //load XML file
       	$this->xml = simplexml_load_file($this->xml_stats);
      }
      
      public function show_ids() {
         if (count($this->xml -> hit) > 0) {
            foreach ($this->xml -> hit as $a) {
               echo $a -> id;
            }//end foreach
         }//end if
      } //end show_ids
   }//end class
?>

Link to comment
Share on other sites

class retrieveStats {
	//set XML file
	public $xml_stats = "./visits.xml";
	private $xml;

	//load XML file
	public function load_xml()
	{
		$this->xml = simplexml_load_file($this->xml_stats);
	}

	public function show_ids() {
		if (count($this->xml -> hit) > 0) {
			foreach ($this->xml -> hit as $a) {
				echo $a -> id;
			}//end foreach
		}//end if
	} //end show_ids
}//end class

Edit: MadTechie inb4 me.

 

But I put loading the xml file within a load_xml method instead of the construct because in your example you tried to use a load_xml method.

Link to comment
Share on other sites

@AlexWD only just beat you and you make the same mistake as I did, but I noticed it just as i was about to hit post

$this->xml = simplexml_load_file($xml_stats);

should be

$this->xml = simplexml_load_file($this->xml_stats);

 

BUT when you think about it, it may be better to pass the filename / path across..

 

Link to comment
Share on other sites

Hey, thanks! The error's gone.

 

Would you mind giving a short explanation as to what you did to make it work? I'd like to avoid having the same trouble later on.

 

Also, why the double underscore? I did a quick Google on it and all I could find was that it makes things "magical".

Link to comment
Share on other sites

@AlexWD only just beat you and you make the same mistake as I did, but I noticed it just as i was about to hit post

$this->xml = simplexml_load_file($xml_stats);

should be

$this->xml = simplexml_load_file($this->xml_stats);

 

BUT when you think about it, it may be better to pass the filename / path across..

:P I actually noticed and fixed that before you posted that.

 

@OP:

 

The main thing you'll need to know is how to use $this, $this is used inside of a class to access methods or properties within itself. So if a class has a property of $xml, to access that property from within the class you'd do $this->xml

Link to comment
Share on other sites

The main thing you'll need to know is how to use $this, $this is used inside of a class to access methods or properties within itself. So if a class has a property of $xml, to access that property from within the class you'd do $this->xml

 

Ah, I've been out of writing PHP for a long while and am used to Actionscript classes. I just figured the syntax would be $this -> $xml, but I see how that wouldn't work.

 

Thanks again for all the help guys.

Link to comment
Share on other sites

The main thing you'll need to know is how to use $this, $this is used inside of a class to access methods or properties within itself. So if a class has a property of $xml, to access that property from within the class you'd do $this->xml

 

Ah, I've been out of writing PHP for a long while and am used to Actionscript classes. I just figured the syntax would be $this -> $xml, but I see how that wouldn't work.

 

Thanks again for all the help guys.

 

Yea, I actually don't like that AS OOP allows that. But even in AS OOP you're allowed to do: this.something; just not required to.

Link to comment
Share on other sites

cont...

 

the function __construct() basically means run this when the class is first called, the other thing to note is that you can't call a function from directly inside a class, it need to be in a function,

 

Ah, well good. That's one less function I have to run in the HTML file.

Link to comment
Share on other sites

Alternatively you can name the constructor as the same name as the class. All of this is stuff that you should probably look into: http://us.php.net/zend-engine-2.php

 

Thanks for the link - classes are definitely something I need to work more with in PHP. I'm rewriting a script I wrote in a few hours using functional logic. Messy thing.

 

I'd much rather have it in OOP, still have trouble thinking that way at first, especially after months of not touching PHP.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.