Hi Guys,

Is it possible to create protected/private object attribute variables on the fly...

I want to do something like this:


$variable_name = 'variable_name';
$variable_value = '123'

# Obviously the initial 'private' doesn't work in the real world.
private $object->$variable_name = $variable_value;

Any pointers would be really appreciated.

Cheers ;]

    hmm... why do you want to do that in the first place?

      laserlight;10913365 wrote:

      hmm... why do you want to do that in the first place?

      Hi there, the main reason is so I can dynamically create some object attribute variables and access them via custom getter methods, that I'll be writing. I want them to be private so they are not accessible via a direct call on the object ($object->attribute_name). Both their names and values need to be variable for my requirements.

      Thanks ;]

        alpha_juno wrote:

        Hi there, the main reason is so I can dynamically create some object attribute variables and access them via custom getter methods, that I'll be writing. I want them to be private so they are not accessible via a direct call on the object ($object->attribute_name). Both their names and values need to be variable for my requirements.

        Eh, that is what you are trying to do. What I would like to know is why, in case there is some better way of doing it.

          laserlight;10913371 wrote:

          Eh, that is what you are trying to do. What I would like to know is why, in case there is some better way of doing it.

          Hi again sorry if that was confusing. The reason why, is that I do not want the dynamically created attribute to be gotten or set directly. Only via getter and setter methods, allowing me some encapsulation and preserving (as much as possible) the integrity of the objects data structure.

          Cheers ;]

            alpha_juno wrote:

            The reason why, is that I do not want the dynamically created attribute to be gotten or set directly. Only via getter and setter methods, allowing me some encapsulation and preserving (as much as possible) the integrity of the objects data structure.

            That still does not make sense to me, since the very act of dynamically creating attributes appears to go against the desire to preserve "the integrity of the objects data structure".

            What I suggest is to use an associative array, e.g.,

            class X
            {
                private $attributes = array();
            
            public function get($attribute_name)
            {
                return $this->attributes[$attribute_name];
            }
            
            public function set($attribute_name, $attribute_value)
            {
                $this->attributes[$attribute_name] = $attribute_value;
            }
            }
            
            $x = new X;
            $x->set('variable_name', '123');
            echo $x->get('variable_name');
              14 days later
              Write a Reply...