Creating Dynamic Class Variables – PHP

  php

Q(Question):

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

A(Answer):

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

A(Answer):

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

A(Answer):

"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!

A(Answer):

"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.

A(Answer):

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

A(Answer):

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?

LEAVE A COMMENT