Creating Dynamic Class Variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tjboudreaux

    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
  • FutureShock

    #2
    Re: Creating Dynamic Class Variables

    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.



    Scotty

    Comment

    • BRADINO

      #3
      Re: Creating Dynamic Class Variables



      Comment

      • Oscar Arreyano

        #4
        Re: Creating Dynamic Class Variables


        "BRADINO" <brad@eduintera ctive.comwrote in message
        news:a62d110c-cb1e-42a3-bdcd-8d8b71a1d09b@n3 3g2000pri.googl egroups.com...that's not at all what he's asking about!


        Comment

        • Oscar Arreyano

          #5
          Re: Creating Dynamic Class Variables


          "FutureShoc k" <futureshockx@a tt.netwrote in message
          news:GDVCk.1776 $c45.424@nlpi06 5.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.


          Comment

          • FutureShock

            #6
            Re: Creating Dynamic Class Variables

            Oscar Arreyano wrote:
            "FutureShoc k" <futureshockx@a tt.netwrote in message
            news:GDVCk.1776 $c45.424@nlpi06 5.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

            Comment

            • Gordon

              #7
              Re: Creating Dynamic Class Variables

              On Sep 25, 6:11 pm, tjboudreaux <tjboudre...@gm ail.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?

              Comment

              Working...