3

I would like to access and assign values to private static class properties and I would like to do the assigning using the concept of 'variable variables'. Accessing works, but assigning does not work. I have tried the following:

class AClass {
    private static $testArray = array();

    public static function aFunction() {
        $key = 'something';
        $arrayName = 'testArray';
        $array = self::$$arrayName;
        // accessing:
        $value = $array[$key]; // This works, $value holds what self::testArray['something'] holds.


        // assigning:
        // version 1:
        $array[$key] = $value; // No error, but self::testArray['something'] does not get updated

        // version 2:
        self::$$arrayName[$key] = $value; // Error
    }
}

Also: I had some trouble coming up with a precise and concise title. If you feel like you understand my problem and can think of a better title, pleas suggest it!

3
  • For the version 1, your array may be a copy of the static array, so assignment will be only on local copy. For the version 2, did you try somthing like self::${$arrayName}[$key] ? I think there is a fail on the priority order Commented Mar 29, 2013 at 4:18
  • @MatRt: I hadn't tried it and it worked! Thanks so much. I'll accept it as an answer if you write it as one. Also: why does this work? I thought I knew php pretty well, but I have never seen anything like it. Commented Mar 29, 2013 at 4:22
  • added my comment, you can mark your best answer. To continue... :) Commented Mar 29, 2013 at 4:29

2 Answers 2

2

For the version 1,

Your array may be a copy of the static array, so assignment will be only on local copy. Since PHP 5, object are passed by reference, by default, but I think array still be passed by copy (except if you specific reference with &) - not 100% sure about that point

For the version 2,

You should try self::${$arrayName}[$key]

There is a priority order problem, you want the PHP to evaluate your "var's var" before interpreting the []. Without the {}, PHP is trying to evaluate something like

self::${$arrayName[$key]}

instead of

self::${$arrayName}[$key]
Sign up to request clarification or add additional context in comments.

Comments

1
<?php

class AClass {
private static $testArray = array('something'=>'check');

public static function aFunction() {
    $key = 'something';
    $arrayName = 'testArray';
    $array = self::$$arrayName;
    // accessing:
    $value = $array[$key]; // This works, $value holds what self::testArray['something'] holds.

    // assigning:
    // version 1:
    $array['something'] = 'now'; // No error, but self::testArray['something'] does not get updated

    //updated value;
   // need to assgig the value again to get it updated ......

   /*
      **if $a = '10';
      $b = $a;
      $b = 20 ; // will it update $a ..?? ANSWER is NO
      same logic applies here**

      if you use $b = &$a ;  then case is different
      */

    self::$$arrayName = $array;

    print_r( self::$$arrayName);

    // version 2:

    // since you are using the key also you have to keep arrayName seperate "note {}"
    self::${$arrayName}[$key] = $value; 


    print_r( self::$$arrayName);
}
  }

  $obj = new AClass();
  $obj->aFunction();

  ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.