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

Creating Dynamic Class Variables

Hi,

This might be a crazy question, but does anyone know how to add
variables dynamically to a class. Here's what I want to do.

I have a class Model, which I inherit for interfacing with database
tables, much like a classic MVC pattern. I want to be able dynamically
create a variable for each field in that table. This is sort of what I
tried out http://pastebin.com/m3fed09a8
I just need to know if there is a way to do what is on lines 8 - 10,
and how that can be done.

Thanks
Travis
Sep 25 '08 #1
6 4906
tjboudreaux wrote:
Hi,

This might be a crazy question, but does anyone know how to add
variables dynamically to a class. Here's what I want to do.

I have a class Model, which I inherit for interfacing with database
tables, much like a classic MVC pattern. I want to be able dynamically
create a variable for each field in that table. This is sort of what I
tried out http://pastebin.com/m3fed09a8
I just need to know if there is a way to do what is on lines 8 - 10,
and how that can be done.

Thanks
Travis
Take a look at overloading using the __get and __set methods.

http://www.php.net/manual/en/languag...verloading.php

Scotty
Sep 26 '08 #2

"BRADINO" <br**@eduinteractive.comwrote in message
news:a6**********************************@n33g2000 pri.googlegroups.com...
http://www.bradino.com/php/variable-variables/
that's not at all what he's asking about!
Sep 28 '08 #4

"FutureShock" <fu**********@att.netwrote in message
news:GD****************@nlpi065.nbdc.sbc.com...
tjboudreaux wrote:
>Hi,

This might be a crazy question, but does anyone know how to add
variables dynamically to a class. Here's what I want to do.

I have a class Model, which I inherit for interfacing with database
tables, much like a classic MVC pattern. I want to be able dynamically
create a variable for each field in that table. This is sort of what I
tried out http://pastebin.com/m3fed09a8
I just need to know if there is a way to do what is on lines 8 - 10,
and how that can be done.

Thanks
Travis

Take a look at overloading using the __get and __set methods.

http://www.php.net/manual/en/languag...verloading.php
The overload will do no good as both __get and __set are ONLY called when a
NON-existing interface (property, function, etc.) is referenced by a caller.

To the op...why not use an array whose keys are the field names? Is there
some kind of validation you are going for if a caller changes a value? If
not, an array is the easiest to implement.
Sep 28 '08 #5
Oscar Arreyano wrote:
"FutureShock" <fu**********@att.netwrote in message
news:GD****************@nlpi065.nbdc.sbc.com...
>tjboudreaux wrote:
>>Hi,

This might be a crazy question, but does anyone know how to add
variables dynamically to a class. Here's what I want to do.

I have a class Model, which I inherit for interfacing with database
tables, much like a classic MVC pattern. I want to be able dynamically
create a variable for each field in that table. This is sort of what I
tried out http://pastebin.com/m3fed09a8
I just need to know if there is a way to do what is on lines 8 - 10,
and how that can be done.

Thanks
Travis
Take a look at overloading using the __get and __set methods.

http://www.php.net/manual/en/languag...verloading.php

The overload will do no good as both __get and __set are ONLY called when a
NON-existing interface (property, function, etc.) is referenced by a caller.

To the op...why not use an array whose keys are the field names? Is there
some kind of validation you are going for if a caller changes a value? If
not, an array is the easiest to implement.

Yes you are right, I think an Array would be the better way, I had not
thought of that. He can assemble his list or variables, send them in one
lump sum and extract them in the class. I may have to play around with
that idea for a bit, may come in useful sometime.

Scotty
Sep 28 '08 #6
On Sep 25, 6:11*pm, tjboudreaux <tjboudre...@gmail.comwrote:
Hi,

This might be a crazy question, but does anyone know how to add
variables dynamically to a class. *Here's what I want to do.

I have a class Model, which I inherit for interfacing with database
tables, much like a classic MVC pattern. I want to be able dynamically
create a variable for each field in that table. This is sort of what I
tried outhttp://pastebin.com/m3fed09a8

I just need to know if there is a way to do what is on lines 8 - 10,
and how that can be done.

Thanks

Travis
What about declaring an empty array in your class and storing
variables there as needed?
Sep 28 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tommy Lang | last post by:
Hi everybody! I am trying to learn the basics of C++ myself and have a hard time understanding some stuff like pointers and references etc. I have created a small program that adds two numbers...
3
by: Lance | last post by:
I have quite an odd question. Usually I ask whenever javascript is not acting the way I'd expect. This time I'm trying to figure out why something works when I don't think it should be working! ...
1
by: Will | last post by:
Hi all. I'm learning VB.Net and am developing a WinForms app. I'm trying to make an app that I will use to scan in one or more than on image. I want to use a tabbed interface to hold each image....
21
by: Sharon | last post by:
I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
3
by: EnglishMan69 | last post by:
Hello All, I am using VB2005 Beta 2 in VS 2005 and am running into a small problem. I need to be able to add a picture box to the main form from within a thread. The program goes to a web...
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
17
Motoma
by: Motoma | last post by:
This article is cross posted from my personal blog. You can find the original article, in all its splendor, at http://motomastyle.com/creating-a-mysql-data-abstraction-layer-in-php/. Introduction:...
13
by: jkimbler | last post by:
As part of our QA of hardware and firmware for the company I work for, we need to automate some testing of devices and firmware. Since not everybody here knows C#, I'm looking to create a new...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.