Jump to content

Creating object/class properties dynamically at run time


Recommended Posts

Hi there,

I am just wondering which way is the correct one if you want to create object properties/attribute dynamically at run time

 

using magic method __set(), __get(), for example:

class Foo{
    
        public $data = array();
      
        public function __set($name,$value){
              $this->data[$name] = $value;
        }

        public function __get($name){
              if ( isset($this->data[$name]) ){
                    return $this->data[$name];
              }
        }
}

 

Or using the below way

 

class Foo{

          public function assign(array $data){
                     foreach($data as $key => $value){
                                $this->$key = $value;
                     }
          }

}

 

I just encountered the second way in many places and I am totally confused  :confused: :confused: . Is the __set() method called implicitly by the PHP engine in the second way or what? how come this is considered as a valid code  :confused:?

 

your usual help is appreciated

 

 

 

Hi there,

 

Maybe I'm too tired to understand you properly ;-p but doesn't __construct() do what you want, or have I totally misunderstood your issue; or maybe I should ask, where are you generating these values?

 

Apologies if I have got myself mixed up...

 

Cheers,

Rw

Hi rwwd,

sorry for confusing you  :P ....I am talking about attributes that are not defined in the class definition. For example,

 

var1 and var2 are both explicitly defined in the class. However, var3 will be created dynamically at run time as shown below

class Foo{
      public $var1;
      public $var2;

      function assign_value($key,$value){
              $this->$key = $value;
      }
}

$foo = new Foo();
$foo->assign_value('var1','test1');  // This is okay becuase var one is already defined in the class
$foo->assign_value('var2','test2');  // This is the same as the first one
$foo->assign_value('var3','test3');   // doing this will create a new attributes called var3 and you can refere to it using $this->var3;

 

Now, how come var3 was created at run time without using the magic method __set()? Was it called implicitly by the PHP engine to create var3 at run time?

 

hope i made myself clear  :D

 

 

regards

 

Now, how come var3 was created at run time without using the magic method __set()? Was it called implicitly by the PHP engine to create var3 at run time?

__set() is just a shortcut method, it doesn't have to be used (and in this case it is not), in fact allot of people will avoid these shortcuts as they can make code that little bit harder to read.

In fact it would be a good idea to do:

 

public function __set($key, $value) {
    throw new Exception('Trying to set non-existent property "' . $key . '" on Object ' . __CLASS__);
}

 

To make sure your class is always properly used.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.