OOP Posted September 11, 2010 Share Posted September 11, 2010 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: . 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 ? your usual help is appreciated Quote Link to comment https://forums.phpfreaks.com/topic/213182-creating-objectclass-properties-dynamically-at-run-time/ Share on other sites More sharing options...
rwwd Posted September 11, 2010 Share Posted September 11, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/213182-creating-objectclass-properties-dynamically-at-run-time/#findComment-1110052 Share on other sites More sharing options...
OOP Posted September 11, 2010 Author Share Posted September 11, 2010 Hi rwwd, sorry for confusing you ....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 regards Quote Link to comment https://forums.phpfreaks.com/topic/213182-creating-objectclass-properties-dynamically-at-run-time/#findComment-1110063 Share on other sites More sharing options...
trq Posted September 12, 2010 Share Posted September 12, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/213182-creating-objectclass-properties-dynamically-at-run-time/#findComment-1110147 Share on other sites More sharing options...
ignace Posted September 12, 2010 Share Posted September 12, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/213182-creating-objectclass-properties-dynamically-at-run-time/#findComment-1110160 Share on other sites More sharing options...
OOP Posted September 12, 2010 Author Share Posted September 12, 2010 Thanks so much guys for the clarification...now things are clear regards Quote Link to comment https://forums.phpfreaks.com/topic/213182-creating-objectclass-properties-dynamically-at-run-time/#findComment-1110195 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.