how to access class member with a dot in the name

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
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

how to access class member with a dot in the name

Post by yacahuma »

Hello

I am uploading an xml using simple_xml_load_file. The problem is that one of the data member has a dot in the name

For example I can do this
$obj->Part1->PensionReceivedPreviousYRS->Year OK

but I CANNOT DO
$obj->Part1->Less->B.TaxExempt->Year Error problem with dot.

What is the correct way?

Code: Select all

 
SimpleXMLElement Object ( [CostAnnuity] => SimpleXMLElement Object ( ) [PensionReceivedPreviousYRS] => SimpleXMLElement Object ( [Year] => Array ( [0] => 2010 [1] => 2020 [2] => SimpleXMLElement Object ( ) [3] => SimpleXMLElement Object ( ) [4] => SimpleXMLElement Object ( ) ) [Amount] => Array ( [0] => 4 [1] => 6 [2] => SimpleXMLElement Object ( ) [3] => SimpleXMLElement Object ( ) [4] => SimpleXMLElement Object ( ) [5] => 10 ) ) [Less] => SimpleXMLElement Object ( [A.TaxablePension] => SimpleXMLElement Object ( [Year] => Array ( [0] => 3030 [1] => SimpleXMLElement Object ( ) [2] => SimpleXMLElement Object ( ) [3] => SimpleXMLElement Object ( ) [4] => SimpleXMLElement Object ( ) ) [Amount] => Array ( [0] => 23.67 [1] => SimpleXMLElement Object ( ) [2] => SimpleXMLElement Object ( ) [3] => SimpleXMLElement Object ( ) [4] => SimpleXMLElement Object ( ) [5] => 23.67 ) ) [B.TaxExempt] => SimpleXMLElement Object ( [Year] => Array ( [0] => 4040 [1] => SimpleXMLElement Object ( ) [2] => SimpleXMLElement Object ( ) [3] => SimpleXMLElement Object ( ) [4] => SimpleXMLElement Object ( ) ) [Amount] => Array ( [0] => 9090 [1] => SimpleXMLElement Object ( ) [2] => SimpleXMLElement Object ( ) [3] => SimpleXMLElement Object ( ) [4] => SimpleXMLElement Object ( ) [5] => 9090 ) ) ) [Total] => 9113.67 [CostPension] => 9103.67 [CostRecovered] => 9103.67 ) 
 
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: how to access class member with a dot in the name

Post by Eran »

Try this:

Code: Select all

 
$dotVar = 'B.TaxExempt';
$obj->Part1->Less-> $dotVar ->Year;
 
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: how to access class member with a dot in the name

Post by yacahuma »

Beautiful
Post Reply