TJMAudio Posted December 22, 2006 Share Posted December 22, 2006 Alright, I am decently skilled with PHP, however I am trying to learn some of the more advanced programming methods. What I am trying to learn now is XML parsing and OOP. Question 1:What is the point of setting vars in a class beforehand? Do they set up the $this->var for the functions? I cannot find a very good tutorial for classes, they are all either too simple, or too advanced.Question 2:Is there a simple way to parse an xml file? I keep finding really, really in-depth huge classes that are multi-function used to process XML files, such as news feeds, etc. Are these huge classes required? Again, I can't find a good tutorial for these.Thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/31560-i-need-some-help-with-two-things/ Share on other sites More sharing options...
trq Posted December 22, 2006 Share Posted December 22, 2006 1/ Sometimes it comes in handy to instantiate an object with some default values.2/ Look at the example sin the manual entry for the [url=http://php.net/simplexml]simplexml[/url] extension. They don't get much simpler. Quote Link to comment https://forums.phpfreaks.com/topic/31560-i-need-some-help-with-two-things/#findComment-146250 Share on other sites More sharing options...
TJMAudio Posted December 22, 2006 Author Share Posted December 22, 2006 [quote author=thorpe link=topic=119599.msg490014#msg490014 date=1166762415]1/ Sometimes it comes in handy to instantiate an object with some default values.2/ Look at the example sin the manual entry for the [url=http://php.net/simplexml]simplexml[/url] extension. They don't get much simpler.[/quote]I don't understand them even more now.. People's tutorials I look at have them set like this:[code]var $name;var $time; var $date;[/code]They don't have any values set.. Quote Link to comment https://forums.phpfreaks.com/topic/31560-i-need-some-help-with-two-things/#findComment-146269 Share on other sites More sharing options...
trq Posted December 22, 2006 Share Posted December 22, 2006 Well thats just to make them available to all methods within the class. Quote Link to comment https://forums.phpfreaks.com/topic/31560-i-need-some-help-with-two-things/#findComment-146276 Share on other sites More sharing options...
keeB Posted December 22, 2006 Share Posted December 22, 2006 Here is my XML class..[code]<?php/* * Created on Sep 6, 2006 by keeb * * Description: * */ class xml { private $url; public $data; private $vals; private $debug = false; public function __construct($url) { $this->url = $url; $xml = file_get_contents($url); $data = $this->xml2array($xml); $this->data = $data; if ($this->debug) { print "<pre>"; print_r($data); } } private function &last(&$array){ if (!count($array)) return null; end($array); return $array[key($array)]; } private function parsexml(&$vals, &$dom, &$lev){ do { $curr = current($vals); $lev = $curr['level']; $tag = strtolower($curr['tag']); switch ($curr['type']) { case 'open': if (!isset($dom[$tag])) $dom[$tag] = array(); array_push($dom[$tag], array()); $new =& $this->last($dom[$tag]); next($vals); $this->parsexml(&$vals, $new, $lev); break; case 'cdata': break; case 'complete': if (!isset($dom[$tag])) $dom[$tag] = $curr['value']; else array_push($dom[$tag] , $curr['value']); break; case 'close': return; } } while (next($vals)!==FALSE); } private function xml2array($xml){ $xml_parser = xml_parser_create(); xml_parse_into_struct($xml_parser, $xml, $vals); xml_parser_free($xml_parser); reset($vals); $dom = array(); $lev = 0; $this->parsexml($vals, $dom, $lev); return $dom; } } ?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31560-i-need-some-help-with-two-things/#findComment-146293 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.