473,320 Members | 1,884 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,320 software developers and data experts.

using variable variables with static variable

150 100+
how can i assign value to static var inside class using
variable variables technique ex:

Expand|Select|Wrap|Line Numbers
  1. self::$$var = $value;
Aug 2 '10 #1

✓ answered by dlite922

What you have should work. Here's a test:

Expand|Select|Wrap|Line Numbers
  1. class foo
  2. {
  3.     public static $bar = null; 
  4.  
  5.  
  6.     function setValue($var,$value)
  7.     {
  8.         self::$$var = $value; 
  9.     }
  10. }
  11.  
  12. $test = new foo(); 
  13.  
  14. $test->setValue("bar","It Does Work!"); 
  15.  
  16. echo foo::$bar;  
  17.  
output is:
Expand|Select|Wrap|Line Numbers
  1. It Does Work!
  2.  
Cheers,


Dan

1 1296
dlite922
1,584 Expert 1GB
What you have should work. Here's a test:

Expand|Select|Wrap|Line Numbers
  1. class foo
  2. {
  3.     public static $bar = null; 
  4.  
  5.  
  6.     function setValue($var,$value)
  7.     {
  8.         self::$$var = $value; 
  9.     }
  10. }
  11.  
  12. $test = new foo(); 
  13.  
  14. $test->setValue("bar","It Does Work!"); 
  15.  
  16. echo foo::$bar;  
  17.  
output is:
Expand|Select|Wrap|Line Numbers
  1. It Does Work!
  2.  
Cheers,


Dan
Aug 3 '10 #2

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

Similar topics

0
by: Bryan Green | last post by:
So I'm working on a project for a C# class I'm taking, where I need to keep some running totals via static variables. I need three classes for three different types of objects. The base class and...
3
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /*...
6
by: Michael B Allen | last post by:
I want to initialize a static variable to a "random" value like: static void * get_key(struct dnsreq *req) { static uint16_t next_txnid = (uint32_t)req & 0xFFFF; But gcc gives me an error: ...
3
by: paulw | last post by:
Hi I have a question: main() { static int i; printf("%d\n",i); // should I see see 0 or 5 ??? for (i=5;i<=15;i++) {...} // What's the meaning of static variable in
10
by: Rene | last post by:
I jus realized that I can change the values of "static variables" and "instance variable" through the standard constructor This means that something like this will compile: public class...
3
by: XPhaktor | last post by:
In C#, how do I replace using VB local static variable declarations to handle method reentrancy. Note, if you use a class-scope variable instead of a local one, then you run the risk of...
18
by: Jack | last post by:
Thanks.
6
by: junw2000 | last post by:
When I define a static variable, where is the memory allocated for the static variable? Thanks. Jack
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
2
by: codingjunkie | last post by:
Hi, I wanted to to do the following and was wondering if that is the correct way- // I need to create an instance of another class XYZClass using the member variable m_xyz. The instance ob...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.