I don't know why but I thought I would try and learn a little PHP. So I thought I would try to build a text based link bar along the left hand side of the screen by reading an XML file such as:
Main
Home
News
My Stuff
One
Two
I made two classes but I can't seem to get past an error saying I am calling a method of
a non object or something. I am not sure what the rules are for posting code here, but in the code below I get an error trying to call $tmpLinkBarItem->setProperty( ... );
The error says "Call to a member function on a non-object on line 112"
What I thought was strange was I could rem out either the call to setProperty or the line before it where I extract an object out of the array, I don't get the error. I don't get any results either, but still no error. Could I be looking in the wrong place?
Now granted my sytanx may not be great nor my classes purpose very well defined but I am new to this PHP stuff.
<?php
class TLinkBarGroup
{
var $Items = array();
var $ItemCount = 0;
var $Name = '';
}
class TLinkBarItem
{
var $ItemName = '';
var $ItemTarget = '';
var $ItemLink = '';
function setProperty( $AProperty, $AValue )
{
switch ($AProperty)
{
case "ITEMNAME":
$this->ItemName = $AValue;
Break;
case "ITEMTARGET":
$this->ItemTarget = $AValue;
Break;
case "ITEMLINK":
$this->ItemLink = $AValue;
Break;
}
}
function GetProperty( $AProperty, $AValue )
{
switch ($AProperty)
{
case "ITEMNAME":
return $this->ItemName;
Break;
case "ITEMTARGET":
return $this->ItemTarget;
Break;
case "ITEMLINK":
return $this->ItemLink;
Break;
}
}
}
function GetLinkBarItems( $ALinkBarXMLFilePath )
{
$GroupData = array();
$GroupCount = 0;
$xml_current_tag_state = '';
$CurrentGroup = new TLinkBarGroup;
if( !($fp = fopen( "assets/xml/linkbargroups.xml", "r" )) )
die( "Couldn't open xml file (assets/xml/linkbargroups.xml)" );
function StartElementHandler( $Parser, $ElementName, $ElementAttributes )
{
global $GroupCount;
global $GroupData;
global $xml_current_tag_state;
$xml_current_tag_state = $ElementName;
}
function EndElementHandler( $Parser, $ElementName )
{
global $GroupCount;
global $GroupData;
global $xml_current_tag_state;
global $CurrentGroup;
if( $ElementName == "GROUP" )
$GroupCount++;
if( $ElementName == "ITEM" )
$CurrentGroup->ItemCount++;
$xml_current_tag_state = '';
}
function CharacterDataHandler( $Parser, $Data )
{
global $GroupCount;
global $GroupData;
global $xml_current_tag_state;
global $CurrentGroup;
$tmpLinkBarItem = new TLinkBarItem;
if( $xml_current_tag_state == '' )
Return;
if( $xml_current_tag_state == "GROUPNAME" )
$CurrentGroup->Name = $Data;
if( ($xml_current_tag_state == "ITEMNAME") or
($xml_current_tag_state == "ITEMTARGET") or
($xml_current_tag_state == "ITEMLINK") )
{
$tmpLinkBarItem = $CurrentGroup->Items[$CurrentGroup->ItemCount];
$tmpLinkBarItem->setProperty( $xml_current_tag_state, $Data );
}
}
if( !($XMLParser = xml_parser_create()) )
die("Couldn't create xml parser (linkbaritems)");
xml_set_element_handler( $XMLParser, "StartElementHandler", "EndElementHandler" );
xml_set_character_data_handler( $XMLParser, "CharacterDataHandler" );
while( $Buffer = fread( $fp, 4096 ) )
{
if( !xml_parse($XMLParser, $Buffer, feof($fp)) )
{
break;
}
}
xml_parser_free( $XMLParser );
return $GroupData;
}
?>