Having problems implementing a Class.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
webrock
Forum Newbie
Posts: 1
Joined: Wed Mar 04, 2009 2:12 pm

Having problems implementing a Class.

Post by webrock »

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:

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;
        }
    
    }
 
 
?>
 
This is how I am implementing it:

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
prgmctan
Forum Newbie
Posts: 19
Joined: Mon Feb 23, 2009 11:29 am

Re: Having problems implementing a Class.

Post by prgmctan »

webrock wrote: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:

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;
        }
    
    }
 
 
?>
 
This is how I am implementing it:

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
As a rule, class names should be in caps, such as: ParseText, but it shouldn't make any difference for this. If you called your constructor parseText would that make any difference?
Post Reply