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

Iterated calling static variables from different classes

3
Hi,
I have a small problem:
I'd like to call static variables from different classes - their names are stored in an array. Example of a class:
Expand|Select|Wrap|Line Numbers
  1. class MyClass1{
  2. public static $mysql_table = "table1";
  3. }
Now I would like to do something like this:
Expand|Select|Wrap|Line Numbers
  1. $classes=array("MyClass1","MyClass2","MyClass3");
  2. foreach ($classes as $item)
  3. {
  4. $class_table = $item::$mysql_table;   // buggy line
  5. // more code here: accessing database
  6. }
I know this won't work. The only solution I was able to come up with was eval():

Expand|Select|Wrap|Line Numbers
  1. eval("\$class_table = ".$item."::\$mysql_table;");
  2.  
But eval() is slow and insecure. Does anybody know any better solution?

Thanks in advance.
Juro
Jul 19 '07 #1
3 1503
code green
1,726 Expert 1GB
It looks like your design has added complexity, so that the only way out is to abuse the theory of classes.
There must surely be a simpler solution.
What is your aim and current design?
Jul 19 '07 #2
juro
3
It looks like your design has added complexity, so that the only way out is to abuse the theory of classes.
There must surely be a simpler solution.
What is your aim and current design?
I'm trying to do something following: I have more classes (10+) related to the tables in database. Those classes are extendnig base class called Object which implements global methods like Load(), Save(), Delete(). This problem is especially while using Delete() because I have to delete not only a row in one table, but also every row in other tables which depend on the deleted row.

So i decided to create static variable $dependent_classes in every class, that holds names of classes which must be processed in Delete() operation.

Sure I can also overload Delete() in every class, but this isn't so nice as one Delete(), which would be able to process any class.

The basic question: Is there any posibility to call static variable with name of the class stored inside other variable? Or is this approach wrong?
Jul 19 '07 #3
code green
1,726 Expert 1GB
I have more classes (10+) related to the tables in database.
I don't like the sound of this bit. A class for each table? What happens when new tables are needed? Surely you want a class, then instatiate an object of that class for each table. Not even this should be considered. An object for each database yes. Then each object has full functionallity of the class. If a particular table needs particular treatment this should be a seperate class method. I would have a generic class to handle basic database functionality - update, delete, insert. Then a special class that inherits the first with individual database funtionality. This may not fit in with your design but it is more like the OO programming model.
Jul 20 '07 #4

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...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
4
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...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
9
by: Chuck Cobb | last post by:
I am creating some static classes with static methods and static variables, but I have a question: What is the lifetime of static classes and static variables? Is there any risk that these...
1
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
The code below is pretty simple. Calling Talker() in the parent returns "Parent", and calling Talker() in the child returns "Child". I'm wondering how I can modify the code so that a call to the...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.