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. -
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];
-
- //how can one of the day variables be updated using variable variable here
-
$$thisSubTotal += $cVal;
-
-
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;
-
}
-
}
-
}
-
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) -
$this->{$shortDay[$thisDay]} += $cVal;
-
2) (looking almost identical) -
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
Thanks for the quick and quite complete answer. This was a great help.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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'
|
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>
|
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.
|
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
|
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
| |
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.
|
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")
|
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?
|
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.,)
|
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:
|
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...
| |
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,...
|
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,...
|
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...
|
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...
|
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();...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |