You are here: abstract class in php5 and xml parsing problem « PHP Language « IT news, forums, messages
abstract class in php5 and xml parsing problem

Posted by Olivier on 05/10/05 02:18

Hi coders!
first of all :
<?php
//abstract classed used for all XML parsers on this site
abstract class MERLINXMLTemplate {
private $tpl;//the actual xml resource
private $file;//path to file
private $xmldata;//file_get_contents

private $status = 0;//TODO define these symbols:
/*0 - OK 1 - FILE_NOT_FOUND, 2 - FILE_NOT_READABLE, 3 -
EXTERNAL_LOAD_HACK
4 - ILLEGAL_ENCODING, 5 - START_END_HANDLER_SETUP_ERROR, 6 -
DATA_HANDLER_SETUP_ERROR
*/
private $encoding = 'ISO-8859-1';
function MERLINXMLTemplate()
{
$this -> __construct();
register_shutdown_function(array($this,"__destruct"));
}
function __construct()
{
//constructor
$this->tpl = xml_parser_create($this->encoding);
echo self;
}
function __destruct()
{
//destructor
}
function free() {
xml_parser_free($this->tpl);
}
function load($file)
{
//REGEX below checks stops URL wrappers
if(!preg_match_all("/\b:\/\/.*\b/si",$file,$t))
{
if(file_exists($file))
{
if(is_readable($file))
{
//file is there, ready to get read
$this->xmldata = file_get_contents($file);
}
else
{
$this->status = 2;
//TODO error handling, file not readable (use exceptions?)
}
}
else
{
$this->status = 1;
//TODO error handling, file not found (use Exceptions?)
}
}
else
{
$this->status=3;
//TODO error handling, internal hacking attempt - url wrapper (use
exceptions?)
}

}
function getStatus() {
return $this->status;
}
function getEncoding(){
return $this->encoding;
}
function setEncoding($encoding)
{
switch($encoding)
{
case 'ISO-8859-1':
$this->encoding = 'ISO-8859-1';
break;
case 'UTF-8':
$this->encoding = 'UTF-8';
break;
case 'US-ASCII';
$this->encoding = 'US-ASCII';
break;
default:
//TODO don't change encoding, but report attack, change status
$this->status = 4;
}
}
//method used to parse it
function parse()
{
if(xml_set_element_handler($this->tpl,array($this,"shdl"),array($this,"ehdl")))
{
if(xml_set_character_data_handler($this->tpl,array($this,"dhdl")))
{
$t = xml_parse($this->tpl,$this->xmldata);
if($t == FALSE)
{
//TODO ERROR handling
echo 'error: '.xml_get_error_code($this->tpl).' =
'.xml_error_string(xml_get_error_code($this->tpl));
}
else
{
//all data parsed ok
}
}
else
{
//TODO error reporting, could not set data handler
$this->status = 6;
}
}
else
{
//TODO error reporting, could not set start and end handlers
$this->status = 5;
}
}
abstract private function shdl($tpl,$name,$attrs);
abstract private function ehdl($tpl,$name);
abstract private function dhdl($tpl,$data);
}


class MERLINXMLLayout extends MERLINXMLTemplate
{
function shdl($tpl,$name,$attrs)
{
//echo
}
function ehdl($tpl,$name)
{
//do stuff
}
function dhdl($tpl,$data)
{
//do stuff
}
}

$xml = new MERLINXMLLayout();
$xml -> load("example.xml");
$xml -> parse();
$xml -> free();

?>

the problem is everything below the setEncoding() method. php says:
Fatal error: Cannot call abstract method MERLINXMLTemplate::shdl() in /file/
on line x
but i want xml_set_element_handler() to call the methods defined in the
inherited class.
Anyone who has any idea of how this could be achieved?
Thanks

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация