PHP Classes

Dynamic Accessing

Recommend this page to a friend!

      GNUPG  >  All threads  >  Dynamic Accessing  >  (Un) Subscribe thread alerts  
Subject:Dynamic Accessing
Summary:Dynamic Accessing of Variables.
Messages:1
Author:Narasimman
Date:2005-12-30 11:27:36
 

  1. Dynamic Accessing   Reply   Report abuse  
Picture of Narasimman Narasimman - 2005-12-30 11:27:36
I want to set the class variables within the class function using the following method. But it not working.
Please help me. Assume variable names and sending variable names($spider->nodeName) are same.

I am using php-5.1.1. the following code is not working. please try to correct suggestion for me.
file :: configspider.inc
<?
class SpiderConfig
{
var $SpiderName=0;
var $SPID;
var $MachineId;
var $Accum_Id;
var $SendFrequency;
var $SpiderVoltageRange;
var $MaxRecord;
var $AccmDataPort;
var $SpiderControlPort;
var $Enabled;
var $SpiderNodeNum;
var $CTCNodeNum1;
var $CTCNodeNum2;
var $Status;
var $SpiderMac;
var $CTCMac1;
var $CTCMac2;
var $SpiderIP;
var $BaseDir;
var $ControllerType;

function SetVar($key,$value)
{
$this->{$key}=$value;
$this->{$key}=10;
}
function GetSpiderConfigFromXML(DOMNodeList $SpConfig)
{
$class_vars = get_class_vars(get_class($this));
foreach($SpConfig as $config)
{
foreach($config->childNodes as $spider)
{
if($spider->nodeType==1)
{
echo "$spider->nodeName ---- : $spider->nodeValue";
echo "<BR>";
$this->SetVar($spider->nodeName,$spider->nodeValue);
}
}
}
}

}
?>

file :: testspider.php
include 'configspider.inc';

$configxmlfile="sample.xml";
define("CONFIG_SPIDERS","TBL_CONFIG_SPIDERS");
define("CFGSPIDER","CONFIG_SPIDERS");
////////////////Command Declarations


$dom = new DomDocument();
$dom->load($configxmlfile);
$SpConfig=new SpiderConfig;
$SpConfig->GetSpiderConfigFromXML($dom->getElementsByTagName(CFGSPIDER));

$class_vars = get_class_vars(get_class($SpConfig));

foreach ($class_vars as $name => $value)
{
echo " $name = $value ";
echo " <BR>" ;
}

?>



The Sample.xml look like this :
<root>
<TBL_CONFIG_SPIDERS>
<CONFIG_SPIDERS>
<SPID>25</SPID>
<MachineId>44</MachineId>
<Accm_Id>2</Accm_Id>
<SendFrequency>1</SendFrequency>
<SpiderVoltageRange>1</SpiderVoltageRange>
<MaxRecord>600</MaxRecord>
<AccmDataPort>9020</AccmDataPort>
<SpiderControlPort>4002</SpiderControlPort>
<Enabled>true</Enabled>
<SpiderNodeNum>0</SpiderNodeNum>
<CTCNodeNum1>0</CTCNodeNum1>
<CTCNodeNum2>0</CTCNodeNum2>
<Status>1</Status>
<SpiderMac />
<CTCMac1 />
<CTCMac2 />
<SpiderIP>10.20.53.21</SpiderIP>
<SpiderName>SP10028</SpiderName>
<BaseDir />
<LastMUId>10735</LastMUId>
<LastMUDId>309975</LastMUDId>
<LastEventId>15190</LastEventId>
<ControllerType>4</ControllerType>
</CONFIG_SPIDERS>
</TBL_CONFIG_SPIDERS>
</root>