Using $$ variable variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    Using $$ variable variable

    I have a method in a class that updates one of 7 variables/properties in the class. I would like to reduce the "switch" command to one line by using a variable variable, but I don't seem to be able to make it work.

    The value passed to the function/method is the day of the week "Sunday", "Monday", ...

    My sample code below returns "variable undefined" on the $$thisSubTotal += $cVal;
    line

    The php manual explains it as this:
    Class properties may also be accessed using variable property names. The variable property name will be resolved within the scope from which the call is made. For instance, if you have an expression such as $foo->$bar, then the local scope will be examined for $bar and its value will be used as the name of the property of $foo. This is also true if $bar is an array access.

    This explanation does not make any sense to me, so this is where I need help.




    Code:
    class reportFunctions
    {
      public $cSun = 0.00;           
      public $cMon = 0.00;           
      public $cTue = 0.00;           
      public $cWed = 0.00;           
      public $cThu = 0.00;           
      public $cFri = 0.00;           
      public $cSat = 0.00;           
    
      function dayDetail($thisDay)
      {
        $cVal = 1;
    
        $shortDay = array('Sunday'=>'cSun','Monday'=>'cMon','Tuesday'=>'cTue','Wednesday'=>'cWed','Thursday'=>'cThu','Friday'=>'cFri','Saturday'=>'cSat');
        $thisSubTotal = '$this->'.$shortDay[$thisDay];
    
    [B]//how can one of the day variables be updated using variable variable here
      $$thisSubTotal += $cVal;[/B]
    
        switch ($thisDay)
        {
          case 'Sunday':
            $this->cSun += $cVal;
    	break;
          case 'Monday':
    	$this->cMon += $cVal;
    	break;
          case 'Tuesday':
    	$this->cTue += $cVal;
    	break;
          case 'Wednesday':
    	$this->cWed += $cVal;
    	break;
          case 'Thursday':
    	$this->cThu += $cVal;
    	break;
          case 'Friday':
    	$this->cFri += $cVal;
    	break;
          case 'Saturday':
    	$this->cSat += $cVal;
    	break;
        }
      }
    }
    Last edited by Claus Mygind; Aug 28 '14, 06:34 PM. Reason: 7 variables not 5
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    there are two options to consider:
    1) using variable properties
    2) using an array

    hence
    1)
    Code:
    $this->{$shortDay[$thisDay]} += $cVal;
    2) (looking almost identical)
    Code:
    class Test
    {
        // property definition
        private $days = [
            'Sunday' => 0,
            'Monday' => 0,
            // etc.
        ];
    
        // setting the property
        public function __set($name, $value)
        {
            if (array_key_exists($name, $this->days) {
                $this->days[$name] += (float) $value;
            }
        }
    
        // accessing the property
        public function __get($name)
        {
            if (array_key_exists($name, $this->days) {
                return $this->days[$name];
            }
            return null;
        }
    }
    of course you could use explicit setter/getters instead
    Last edited by Dormilich; Aug 29 '14, 07:03 AM.

    Comment

    • Claus Mygind
      Contributor
      • Mar 2008
      • 571

      #3
      Thanks for the quick and quite complete answer. This was a great help.

      Comment

      Working...