Marooon Posted May 8, 2012 Share Posted May 8, 2012 Dear all , i am trying the following : i have a class named ACCOUNT with many properties in .some of these properties are array , it is like this : class ACCOUNT { PRIVATE $DB_LINK; PRIVATE $COMP; PRIVATE $BRANCH; PRIVATE $CURRENCY; PRIVATE $GL; PRIVATE $CIF; PRIVATE $SL; PRIVATE $EXIST; PRIVATE $STATUS; private $ACCOUNT_NAME=ARRAY("LA"=>'',"LE"=>'',"SA"=>'',"SE"=>''); private $ACCOUNT_BALANCE =ARRAY('FC_YTD','CV_YTD','CV_BAL','YTD_BAL','BLOCKED_CV','BLOCKED_FC'); private $CY_NAME=ARRAY("LA"=>'',"LE"=>'',"SA"=>'',"SE"=>''); private $ACCOUNT_NAME_USR=ARRAY("LA"=>'',"LE"=>'',"SA"=>'',"SE"=>''); private $LEDGER_NAME= ARRAY("LA"=>'',"LE"=>''); i have created the following method to call any property [code] FUNCTION GET_SPECIFEC_ATT($ATT,$LANG) { $ATT=$ATT."['L$LANG']"; ECHO $this->$ATT; } but i am getting the below error : Notice: Undefined property: ACCOUNT::$BRANCH_NAME['LA'] in D:\wamp\www\EBANK\account.class on line 186 if i used this : echo $this->BRANCH_NAME['LA']; it is working fine . and the method is working fine i can iam trying to call property which is NOT an array. Can you please help me in what iam doing wrong ? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/262247-calling-private-array-in-class/ Share on other sites More sharing options...
trq Posted May 8, 2012 Share Posted May 8, 2012 Judging by the code provided, the property $BRANCH_NAME is not defined. ps: What's with all the caps? That's some of the ugliest code Ive seen. Quote Link to comment https://forums.phpfreaks.com/topic/262247-calling-private-array-in-class/#findComment-1343923 Share on other sites More sharing options...
Marooon Posted May 8, 2012 Author Share Posted May 8, 2012 First thanks for your answer ,i am sorry i missed a line while copying it from my code , the last line in the properties is private $BRANCH_NAME= ARRAY("LA"=>'',"LE"=>''); calling it as below is working fine : echo $this->BRANCH_NAME["LA"]; i didnt get you why this code considered ugly Quote Link to comment https://forums.phpfreaks.com/topic/262247-calling-private-array-in-class/#findComment-1343925 Share on other sites More sharing options...
trq Posted May 8, 2012 Share Posted May 8, 2012 If the property really is defined you shouldn't be getting the error you are getting. When asking a question, it helps to post your actual code. As for why I consider your code to be ugly. It's the caps. It is very unconventional and if I had to look at code like that all day, I would quit my job, seriously. Quote Link to comment https://forums.phpfreaks.com/topic/262247-calling-private-array-in-class/#findComment-1343932 Share on other sites More sharing options...
Zephni Posted May 8, 2012 Share Posted May 8, 2012 You should call class properties without the $ <?php $this->ATT; ?> In your function Quote Link to comment https://forums.phpfreaks.com/topic/262247-calling-private-array-in-class/#findComment-1343936 Share on other sites More sharing options...
Marooon Posted May 8, 2012 Author Share Posted May 8, 2012 thank you again That is the problem , it is defined but not working . i will appreciate your help a lot. i will try to reformat the code to be better thank you Quote Link to comment https://forums.phpfreaks.com/topic/262247-calling-private-array-in-class/#findComment-1343937 Share on other sites More sharing options...
Marooon Posted May 8, 2012 Author Share Posted May 8, 2012 Dear Zephni, the ATT is not a property of the class, it a variable used inside this method(function). the $ATT includes the property i want to use . in my case: FUNCTION GET_SPECIFEC_ATT($ATT,$LANG) { $ATT=$ATT."['L$LANG']"; ECHO $this->$ATT; } when i call this function like FUNCTION GET_SPECIFEC_ATT('BRANCH_NAME','A') THE $ATT WILL be equal to BRANCH_NAME['LA'] the echo statment should be : echo $this->BRANCH_NAME['LA'] but iam getting that error thank you Quote Link to comment https://forums.phpfreaks.com/topic/262247-calling-private-array-in-class/#findComment-1343938 Share on other sites More sharing options...
Zephni Posted May 8, 2012 Share Posted May 8, 2012 Could you show me how you are calling that function Quote Link to comment https://forums.phpfreaks.com/topic/262247-calling-private-array-in-class/#findComment-1343939 Share on other sites More sharing options...
trq Posted May 8, 2012 Share Posted May 8, 2012 Why exactly are you going about things so awkwardly? function get_specific_att($att,$lang) { echo $this->$att['L' . $lang]; } Should do what you want. It's a terrible way of writing code though, on many, many levels. Quote Link to comment https://forums.phpfreaks.com/topic/262247-calling-private-array-in-class/#findComment-1343940 Share on other sites More sharing options...
Marooon Posted May 8, 2012 Author Share Posted May 8, 2012 Dear thorpe i am getting the same error Notice: Undefined property: ACCOUNT::$B in D:\wamp\www\EBANK\account.class on line 185 Dear Zephni that it is the way i call the function : function is : FUNCTION GET_SPECIFEC_ATT($ATT,$LANG) { echo $this->$ATT['L' . $LANG]; } how i call it : $ACCOUNTS[$i]=NEW ACCOUNT($CIF_ACCOUNTS[$i]); ECHO $ACCOUNTS[$i]->GET_SPECIFEC_ATT("BRANCH_NAME","A"); thank you for your help Quote Link to comment https://forums.phpfreaks.com/topic/262247-calling-private-array-in-class/#findComment-1343979 Share on other sites More sharing options...
Marooon Posted May 8, 2012 Author Share Posted May 8, 2012 also thorpe , can you please guide me what am i doing wrong ? thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/262247-calling-private-array-in-class/#findComment-1343980 Share on other sites More sharing options...
KevinM1 Posted May 8, 2012 Share Posted May 8, 2012 All capital letters, at least in the West, = shouting. Plus, it makes code hard to read as every letter has a uniformity in size when capitalized. Since code is as much for humans to read (as we're trying to do here) as it is for computers to process, it makes sense to write code that's readable. That means stop using all caps. Quote Link to comment https://forums.phpfreaks.com/topic/262247-calling-private-array-in-class/#findComment-1343981 Share on other sites More sharing options...
Marooon Posted May 8, 2012 Author Share Posted May 8, 2012 I Think i figured out what is the problem in this case echo $this->$ATT['L' . $LANG]; it is dealing with $ATT as an array and trying to find the offset LA, which doesnt exist i tried to do that FUNCTION GET_SPECIFEC_ATT($ATT,$LANG) { $ATT=$ATT.'["LA"]'; ECHO $ATT; echo $this->$ATT; } but i am getting this error now : Notice: Undefined property: ACCOUNT::$BRANCH_NAME["LA"] in D:\wamp\www\EBANK\account.class on line 189 Please help Quote Link to comment https://forums.phpfreaks.com/topic/262247-calling-private-array-in-class/#findComment-1343982 Share on other sites More sharing options...
ignace Posted May 8, 2012 Share Posted May 8, 2012 WE CAN NOT HELP YOU IF YOU KEEP WITHOLDING INFORMATION. I ALSO TOOK UP CAPS FOR WRITING SINCE YOU ARE SO USED TO IT, I HOPE YOU DON'T MIND. IMO ARE YOU DOING THIS: $ACCOUNT->GET_SPECIFEC_ATT('$BRANCH_NAME', ''); IT SHOULD BE $ACCOUNT->GET_SPECIFEC_ATT('BRANCH_NAME', ''); Quote Link to comment https://forums.phpfreaks.com/topic/262247-calling-private-array-in-class/#findComment-1344080 Share on other sites More sharing options...
Jessica Posted May 8, 2012 Share Posted May 8, 2012 Specific. Quote Link to comment https://forums.phpfreaks.com/topic/262247-calling-private-array-in-class/#findComment-1344085 Share on other sites More sharing options...
xyph Posted May 9, 2012 Share Posted May 9, 2012 thorpe, I'm ashamed! I've seen your framework, and I hold you to a much higher standard than that You have to use curly braces, or PHP thinks you're asking for the character in $ATT located at $LANG offset echo $this->{$ATT}['L'.$LANG] A working, standalone example <?php class foobar { private $array = array('greet'=>'Hello World'); public function get_private_property( $prop, $key ) { if( !isset($this->$prop) || !is_array($this->$prop) || !isset($this->{$prop}[$key]) ) return FALSE; return $this->{$prop}[$key]; } } $obj = new foobar; $result = $obj->get_private_property('array', 'greet'); if( !$result ) echo 'It doesnt exist'; else echo $result; ?> Using variable variables (or properties) can get messy, and for reasons like the confusion above, it's a good idea to avoid using them. Debugging NIGHTMARE Quote Link to comment https://forums.phpfreaks.com/topic/262247-calling-private-array-in-class/#findComment-1344112 Share on other sites More sharing options...
Marooon Posted May 9, 2012 Author Share Posted May 9, 2012 first i am sorry for the upper case issue , i used to code with Upper case due to the case sensitive issue. i found a work around as below : FUNCTION GET_SPECIFEC_ATT($ATT,$LANG) { $ATT=$this->$ATT; IF (is_array($ATT)) { return $ATT["L".$LANG]; } else { return $ATT; } } also i tried xyph solution it is working thank you all alot , Quote Link to comment https://forums.phpfreaks.com/topic/262247-calling-private-array-in-class/#findComment-1344172 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.