Jump to content

Calling private array in class


Marooon

Recommended Posts

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

Link to comment
Share on other sites

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  :-[  :-[

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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', '');

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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 :D

 

thank you all alot ,

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.