By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

Creating Dynamic Class Variables

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a

"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

P: n/a

"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.