473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Using $$ variable variable

Claus Mygind
571 512MB
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.




Expand|Select|Wrap|Line Numbers
  1. class reportFunctions
  2. {
  3.   public $cSun = 0.00;           
  4.   public $cMon = 0.00;           
  5.   public $cTue = 0.00;           
  6.   public $cWed = 0.00;           
  7.   public $cThu = 0.00;           
  8.   public $cFri = 0.00;           
  9.   public $cSat = 0.00;           
  10.  
  11.   function dayDetail($thisDay)
  12.   {
  13.     $cVal = 1;
  14.  
  15.     $shortDay = array('Sunday'=>'cSun','Monday'=>'cMon','Tuesday'=>'cTue','Wednesday'=>'cWed','Thursday'=>'cThu','Friday'=>'cFri','Saturday'=>'cSat');
  16.     $thisSubTotal = '$this->'.$shortDay[$thisDay];
  17.  
  18. //how can one of the day variables be updated using variable variable here
  19.   $$thisSubTotal += $cVal;
  20.  
  21.     switch ($thisDay)
  22.     {
  23.       case 'Sunday':
  24.         $this->cSun += $cVal;
  25.     break;
  26.       case 'Monday':
  27.     $this->cMon += $cVal;
  28.     break;
  29.       case 'Tuesday':
  30.     $this->cTue += $cVal;
  31.     break;
  32.       case 'Wednesday':
  33.     $this->cWed += $cVal;
  34.     break;
  35.       case 'Thursday':
  36.     $this->cThu += $cVal;
  37.     break;
  38.       case 'Friday':
  39.     $this->cFri += $cVal;
  40.     break;
  41.       case 'Saturday':
  42.     $this->cSat += $cVal;
  43.     break;
  44.     }
  45.   }
  46. }
  47.  
Aug 28 '14 #1

✓ answered by Dormilich

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

hence
1)
Expand|Select|Wrap|Line Numbers
  1. $this->{$shortDay[$thisDay]} += $cVal;
  2.  
2) (looking almost identical)
Expand|Select|Wrap|Line Numbers
  1. class Test
  2. {
  3.     // property definition
  4.     private $days = [
  5.         'Sunday' => 0,
  6.         'Monday' => 0,
  7.         // etc.
  8.     ];
  9.  
  10.     // setting the property
  11.     public function __set($name, $value)
  12.     {
  13.         if (array_key_exists($name, $this->days) {
  14.             $this->days[$name] += (float) $value;
  15.         }
  16.     }
  17.  
  18.     // accessing the property
  19.     public function __get($name)
  20.     {
  21.         if (array_key_exists($name, $this->days) {
  22.             return $this->days[$name];
  23.         }
  24.         return null;
  25.     }
  26. }
  27.  
of course you could use explicit setter/getters instead

2 1374
Dormilich
8,658 Expert Mod 8TB
there are two options to consider:
1) using variable properties
2) using an array

hence
1)
Expand|Select|Wrap|Line Numbers
  1. $this->{$shortDay[$thisDay]} += $cVal;
  2.  
2) (looking almost identical)
Expand|Select|Wrap|Line Numbers
  1. class Test
  2. {
  3.     // property definition
  4.     private $days = [
  5.         'Sunday' => 0,
  6.         'Monday' => 0,
  7.         // etc.
  8.     ];
  9.  
  10.     // setting the property
  11.     public function __set($name, $value)
  12.     {
  13.         if (array_key_exists($name, $this->days) {
  14.             $this->days[$name] += (float) $value;
  15.         }
  16.     }
  17.  
  18.     // accessing the property
  19.     public function __get($name)
  20.     {
  21.         if (array_key_exists($name, $this->days) {
  22.             return $this->days[$name];
  23.         }
  24.         return null;
  25.     }
  26. }
  27.  
of course you could use explicit setter/getters instead
Aug 29 '14 #2
Claus Mygind
571 512MB
Thanks for the quick and quite complete answer. This was a great help.
Aug 29 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: LVande | last post by:
SQLLY challenged be gentle -- Trying to create code that will drop a table using a variable as the Table Name. DECLARE @testname as char(50) SELECT @testname = 'CO_Line_of_Business_' +...
3
by: Mike Conmackie | last post by:
Greetings, I am trying to create a node in the output tree using a variable. Here are some fragments that I hope will explain the problem better. <xsl:stylesheet...
2
by: littlefitzer | last post by:
Hi, Say for example I want to use the following line in my XSL: <xsl:call-template name="buildText"/> I know I have to have a template defined named buildText. My question is: Is there a way...
9
by: Stefan Mueller | last post by:
I'd like to set a variable called 'FocusIsOn' if a button got the focus. Because my button is dynamically created I do it like xelement = document.createElement("input") xelement.type = "button"...
8
by: chellappa | last post by:
hi Everybody ! decalaring variable in a.h and using that vaaariable in a1.c and inalization is in main.c it is possible .........pleaase correct that error is it Possible? i am trying it...
1
by: Epson Barnett | last post by:
Hi, I'm new to C# and I'd like to be able to reference a field of an object using a variable instead of literal text. In the PHP scripting language, you can create a variable: $var = "name";...
1
by: maheshd | last post by:
Hai I had the problem of using the variable in asp code that had passed in java script function Here the code i used function fun(form2,cname) { alert(cnme.value) <% set...
7
by: John Smith | last post by:
Hello, I have a simple question, I have a vb.net form with several buttons. If I store the name of a button in a variable.. Dim TheName as string TheName = ...
7
by: sachsase | last post by:
//swap of 2 variables void main() { int a=27,b=46; a=1?a-(b=1?((a=1?a+b:0)-b):0):0; printf("a=%db=%d",a,b); } I got output for this problem as a=46;b=27 Same but using scanf to read...
8
by: jj | last post by:
Trying to understand the manual regarding variables to solve the following. I'm working with Drupal CMS and have a loop to retrieve values that are normally fetched by using this variable:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.