scheda Posted September 27, 2009 Share Posted September 27, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/175670-solved-having-trouble-with-simplexml/ Share on other sites More sharing options...
MadTechie Posted September 27, 2009 Share Posted September 27, 2009 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/175670-solved-having-trouble-with-simplexml/#findComment-925658 Share on other sites More sharing options...
Alex Posted September 27, 2009 Share Posted September 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/175670-solved-having-trouble-with-simplexml/#findComment-925660 Share on other sites More sharing options...
MadTechie Posted September 27, 2009 Share Posted September 27, 2009 @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.. Quote Link to comment https://forums.phpfreaks.com/topic/175670-solved-having-trouble-with-simplexml/#findComment-925662 Share on other sites More sharing options...
scheda Posted September 27, 2009 Author Share Posted September 27, 2009 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". Quote Link to comment https://forums.phpfreaks.com/topic/175670-solved-having-trouble-with-simplexml/#findComment-925664 Share on other sites More sharing options...
Alex Posted September 27, 2009 Share Posted September 27, 2009 @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.. 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 Quote Link to comment https://forums.phpfreaks.com/topic/175670-solved-having-trouble-with-simplexml/#findComment-925668 Share on other sites More sharing options...
scheda Posted September 27, 2009 Author Share Posted September 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/175670-solved-having-trouble-with-simplexml/#findComment-925670 Share on other sites More sharing options...
Alex Posted September 27, 2009 Share Posted September 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/175670-solved-having-trouble-with-simplexml/#findComment-925672 Share on other sites More sharing options...
MadTechie Posted September 27, 2009 Share Posted September 27, 2009 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, Quote Link to comment https://forums.phpfreaks.com/topic/175670-solved-having-trouble-with-simplexml/#findComment-925673 Share on other sites More sharing options...
scheda Posted September 27, 2009 Author Share Posted September 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/175670-solved-having-trouble-with-simplexml/#findComment-925676 Share on other sites More sharing options...
Alex Posted September 27, 2009 Share Posted September 27, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/175670-solved-having-trouble-with-simplexml/#findComment-925677 Share on other sites More sharing options...
scheda Posted September 27, 2009 Author Share Posted September 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/175670-solved-having-trouble-with-simplexml/#findComment-925680 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.