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!

tshepang
  • 12,111
  • 21
  • 91
  • 136
DudeOnRock
  • 3,685
  • 3
  • 27
  • 58
  • 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 – MatRt Mar 29 '13 at 04: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. – DudeOnRock Mar 29 '13 at 04:22
  • added my comment, you can mark your best answer. To continue... :) – MatRt Mar 29 '13 at 04:29

2 Answers2

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]
MatRt
  • 3,494
  • 1
  • 19
  • 14
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();

  ?>
alwaysLearn
  • 6,882
  • 7
  • 39
  • 67