Connecting Tech Pros Worldwide Forums | Help | Site Map

Creating Dynamic Class Variables

tjboudreaux
Guest
 
Posts: n/a
#1: Sep 25 '08
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

FutureShock
Guest
 
Posts: n/a
#2: Sep 26 '08

re: Creating Dynamic Class Variables


tjboudreaux wrote:
Quote:
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
BRADINO
Guest
 
Posts: n/a
#3: Sep 26 '08

re: Creating Dynamic Class Variables


http://www.bradino.com/php/variable-variables/

Oscar Arreyano
Guest
 
Posts: n/a
#4: Sep 28 '08

re: Creating Dynamic Class Variables



"BRADINO" <brad@eduinteractive.comwrote in message
news:a62d110c-cb1e-42a3-bdcd-8d8b71a1d09b@n33g2000pri.googlegroups.com...that's not at all what he's asking about!


Oscar Arreyano
Guest
 
Posts: n/a
#5: Sep 28 '08

re: Creating Dynamic Class Variables



"FutureShock" <futureshockx@att.netwrote in message
news:GDVCk.1776$c45.424@nlpi065.nbdc.sbc.com...
Quote:
tjboudreaux wrote:
Quote:
>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.


FutureShock
Guest
 
Posts: n/a
#6: Sep 28 '08

re: Creating Dynamic Class Variables


Oscar Arreyano wrote:
Quote:
"FutureShock" <futureshockx@att.netwrote in message
news:GDVCk.1776$c45.424@nlpi065.nbdc.sbc.com...
Quote:
>tjboudreaux wrote:
Quote:
>>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
Gordon
Guest
 
Posts: n/a
#7: Sep 28 '08

re: Creating Dynamic Class Variables


On Sep 25, 6:11*pm, tjboudreaux <tjboudre...@gmail.comwrote:
Quote:
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?
Closed Thread