Having problems implementing a Class.
Posted: Tue Mar 10, 2009 9:32 am
Hi, I'm new to the board but have been looking as a guest for a while. I am new to PHP OOP coding and I am having a problem implementing a class. I have searched to forum but have not been able to find anything that will help me solve this. I have been able to get this bit of code working on a single page but not as a function or within a class. I am parsing an XML file and want to return an array of certain tag contents. As I said I can get it working when not inside of a class. I don't get any kind of error message just no data in the array.
Here is the class:
This is how I am implementing it:
Thanks for any help
Walt
Here is the class:
Code: Select all
<?php
class parseTest
{
function __construct($filename)
{
$this->parser = xml_parser_create();
xml_set_object($this->parser, $this);
xml_set_element_handler($this->parser, "start_tag", "end_tag");
xml_set_character_data_handler($this->parser, "tag_contents");
if (!($fp = fopen($filename, "r")))
{
die("cannot open ".$filename);
}
while ($data = fread($fp, 4096))
{
$data=eregi_replace(">"."[[:space:]]+"."<","><",$data);
if (!xml_parse($this->parser, $data, feof($fp)))
{
$reason = xml_error_string(xml_get_error_code($this->parser));
$reason .= xml_get_current_line_number($this->parser);
die($reason);
}
}
}
function start_tag($parser, $name, $attribs)
{
global $current;
$current = $name;
switch ($name)
{
case 'WEBORDER_NUMBER':
break;
}
}
function tag_contents($parser, $data)
{
global $current;
switch ($current)
{
case 'WEBORDER_NUMBER':
$orderNum[] = $this->data;
break;
}
}
function end_tag($parser, $name)
{
global $name;
switch ($name)
{
case 'WEBORDER_NUMBER':
break;
}
}
function getOrderNum()
{
return $this->orderNum;
}
}
?>
Code: Select all
<?php
$objTest = new parseTest('ORDE3238.xml');
$anotherTest = $objTest->getOrderNum();
if (!$anotherTest)
{
echo "No Data was returned.";
}
else
{
print_r($anotherTest);
}
?>
Thanks for any help
Walt