Zeka Posted October 18, 2018 Share Posted October 18, 2018 Hi. In different modules, I have seen these two ways how default data is set static public function getDefaultData() { return array( "test" => "test" ); } // first way public function __construct() { foreach (self::getDefaultData() as $key => $value) { $this->$key = $value; } } // second way public function __construct() { foreach (self::getDefaultData() as $key => $value) { $this->set($key, value); } } Could somebody please explain what is the difference between them? Link to comment Share on other sites More sharing options...
kongondo Posted October 18, 2018 Share Posted October 18, 2018 (edited) There is very little difference: http://processwire.com/api/ref/wire-data/set/ This one sets the property value directly. The $key is converted to a string (e.g. $this->myString = 'my value';) $this->$key = $value; This one uses the method set() to set both a key and a value for you. I'm not sure if the $key is manipulated in anyway behind the scenes, e.g make it 'property' friendly. I've had a quick look and couldn't find anything to that effect. $this->set($key, value); You can also do (Set a property using array access) : $this[$key] = $value; Edit: Reading your thread title, there is no right way. All ways are correct and are documented ? Edited October 18, 2018 by kongondo 2 Link to comment Share on other sites More sharing options...
Soma Posted October 18, 2018 Share Posted October 18, 2018 There's no difference only preference. 2 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now