Does storing config values in protected vars makes sense?
I'm toying around with the recent discussion list and want to add content to each discussion, based on a config setting.
Getting values from the config needs accessing config-defaults.php
as well as config.php
- at least that's what I think is true when you are using DirtyCache - and so I hope there is a possibility to read that value only one time and store it for all usages on that page access.
I'm thinking of something like that in my plugin:
protected $_ConfigValue; public function __construct() { $this->$_ConfigValue = Gdn::Config('Plugins.MyPlugin.ConfigValue', 250); } public function discussionsController_afterDiscussionContent_handler($Sender) { // Now using $this->$_ConfigValue instead of // Gdn::Config('Plugins.MyPlugin.ConfigValue', 250) }
Would that have any advantages over simply using Gdn::Config()?
I do not have any knowledge about the "lifetime" of my class. And if my class is constructed more than one time when recent discussions is build up, it would make no sense at all, I think. But when my function __construct is only called once in recent discussions, it would be clever, wouldn't it?
Best Answer
-
x00 MVP
First protected/private properties aren't actually to do with security, like some people might think. Take python there is no such distinction between properties, it is up to you to enforce any relationship you'd need, without requiring strict language level control.
Secondly the difference is once loaded config values are just held in a global array. If you store in properties, then assuming the config is not set to somethign else then it will remain the same. It is when one is set then they will be different.
Configs are all full saved, on the shutdown of the php process. So even it is it set in memory it isn't saved to file till the end.
Is there any advantage? not really. Except, it might be a be shorter and clearer to use a variable or property.
grep is your friend.
5
Answers
First protected/private properties aren't actually to do with security, like some people might think. Take python there is no such distinction between properties, it is up to you to enforce any relationship you'd need, without requiring strict language level control.
Secondly the difference is once loaded config values are just held in a global array. If you store in properties, then assuming the config is not set to somethign else then it will remain the same. It is when one is set then they will be different.
Configs are all full saved, on the shutdown of the php process. So even it is it set in memory it isn't saved to file till the end.
Is there any advantage? not really. Except, it might be a be shorter and clearer to use a variable or property.
grep is your friend.
Thanks for that! I found it not being shorter, but bloated - but it made me feel like a genius for a few minutes...
I get that feeling every time I discover some core functionality, only to realize I used it in another plugin already!
Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
both you guys @r_j and @hgtonight are genuises for well over an hour extending into days and weeks and beyond. Always interesting posts and solutions.
I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.