473,588 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using $$ variable variable

Claus Mygind
571 Contributor
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
2 1383
Dormilich
8,658 Recognized Expert Moderator Expert
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 Contributor
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
8353
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_' + SUBSTRING(CAST(CD_LAST_EOM_DATE AS varchar), 5, 2) + '_' + LEFT(CAST(CD_LAST_EOM_DATE AS varchar), 4)+ '_' + 'EOM'
3
1950
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 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:glbl="http://www.compuware.com/XSL/globalvariables" version="1.0"> <glbl:host-id-types> <glbl:host-id-type t="undefined">unspecified</glbl:host-id-type>
2
1418
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 to parse similarly named variables efficiently? To elaborate, I had a template called buildText that would build some text for me e.g.
9
1856
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" element.onfocus = function() {FocusIsOn = "Button_1"; alert(FocusIsOn)} That works great. If the button gets the focus, I got a message box saying 'Button_1'. However, if I set the variable 'FocusIsOn' dynamically (using a variable
8
3502
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 gives error! In file included from main.c:2: a1.c:3: error: initializer element is not constant
1
2070
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"; and another variable: $name = "Epson"; Then you can refer to $name, or $$var. They will equal the same thing.
1
1399
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 sql=server.CreateObject("ADODB.RECORDSET")
7
5710
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 = Me.btnMyLittleButton.Name.ToString How can I disable this button using the variable value?
7
2380
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 (i.e.,)
8
1496
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: $node- incrementing that number 1 so next variable is $node- Here's what I'm trying in my loop to change that number in the variable using $i:
0
7927
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7857
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
7981
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8222
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5723
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5396
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3846
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1457
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1194
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.