Opened 7 years ago
Last modified 4 months ago
#45861 new defect (bug)
Global variable in plugin main file
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Awaiting Review | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | Plugins | Keywords: | dev-feedback has-patch 2nd-opinion |
| Focuses: | Cc: |
Description
In the main plugin file, if we declare a variable, it will be used as a global variable in WordPress.
This might lead to conflict for global variable names.
The code is in wp-settings.php file.
A simple fix is creating a function to load a plugin and include main plugin file inside that function. So the scope is function scope, not global scope.
Attachments (1)
Change History (8)
#2
@
7 years ago
@rilwis Just create new function to include plugin file is solve the issue you have created? also you have added @return void in your patch 45861.patch that do not needed as function does not return any return value.
Needs to wait for code developer reply on this ticket
#4
@
7 years ago
@mukesh27 Yes, using a function to include plugin files makes the variables available in the function scope only, which means they're not available in the global scope.
Using @return void is a valid (it's recommended but not a must) since it's the recommended way to tell developers that the function does not return anything.
#5
@
5 months ago
Hi @rilwis,
Just having a look at this as its been a few years, but I wonder if there is a reason to not do this. For example in some cases I can see reason for plugins to expose variables globally, but also I imagine some plugins will work in conjunction with variables from other plugins. However if a plugin developer wanted to obscure their variables, then they can do this within the plugin and wrapping their functionality within a function/class.
Such a sweeping change like this could potentially break the functionality of a wide range of plugins, and I personally would advise against this. However I think this requires further developer input to discuss.
#6
@
5 months ago
Hi @callumbw95,
Thanks for your feedback. You're right about the potential breakage of code if plugin developers intentionally expose the global variable just by declaring it in the main plugin file.
I have no stats about this. It's just my personal opinion and observation that some plugin developers put their random variables in the main plugin files, and that can cause garbage in the global scope, and also potentially break other plugins' functionalities if they share the same names.
If plugin developers want to make a variable global, it's best if they declare it explicitly via $_GLOBAL['my_plugin_slug'] = ....
#7
@
4 months ago
- Keywords 2nd-opinion added
HI @rilwis,
I think it would be good to get a 2nd-opinion here as I am not sure the best way forward with this. I think potentially educating plugin developers within the docs to make sure they don't do this makes a lot of sense. I would also like to put in the patch mentioned, but feel this could end up breaking a lot of plugins without warning, and without properly informing the community that plugins will need to be updated to work with this new code could have major repercussions.
However I have marked this ticket with the 2nd-opinion tag to see if we can get any more feedback on this. 😃
Proposed patch