Come together with the global Drupal community in Rotterdam, 28 Sept – 1 Oct 2026. Sessions, contribution, connection, and Early Bird savings until 8 June.
Declaring a variable to be static does not also make it global. When you call your new function in validate, description, js, etc, it doesn't look like it would actually do anything, so it looks like the only time your new function does anything is when you call it in password_policy.admin.inc.
If validate, description, js etc is the first to call the function _password_policy_contraints(), the static var is by definition not yet set and therefore loads in the contraint inc files. If however the function has already been called once on the page load, the static var is already set and is returned without loading in the constrain inc files. Using global var is overkill especially since we're loading in inc files on 99% of pages that have no need for them.
I'm with you on getting it out of hook_init(), you don't have to justify that part.
What I'm saying is that if you're only using the return value of your new function in admin.inc, then it can load the constraint files then. They don't need to be pre-loaded and set in a static var.
There is a negligible performance hit for the array of drupal_substr($file->name, 11) which as you rightly point out is effectively discarded for all but the admin.inc. The benefit is that the file load logic is consistent throughout. And of course net-net, its a huge performance improvement over loading them for every page.
Function with static var seems a pretty common approach anywhere file io is used.
I'm still not sure why you have all the extra calls. If that function doesn't include the relevant constraint files at the time you called it there, will stuff break?
When I (admin) navigate to admin/settings/password_policy/add, I get the following error:
Fatal error: Call to undefined function _password_policy_constraints() in sites\all\modules\password_policy\password_policy.admin.inc on line 257
I understand the misspelling issue. Can we push this patch to the latest downloadable version? A recent download still gets "foreach (_password_policy_constraints() as $constraint) {" on line 257.
Comments
Comment #1
john.money commentedAttached is patch which moves the global variable from hook_init into a static variable in function _password_policy_contraints().
Comment #2
deekayen commentedDeclaring a variable to be static does not also make it global. When you call your new function in validate, description, js, etc, it doesn't look like it would actually do anything, so it looks like the only time your new function does anything is when you call it in password_policy.admin.inc.
Comment #3
john.money commentedThe static var gets set just once per page load.
If validate, description, js etc is the first to call the function _password_policy_contraints(), the static var is by definition not yet set and therefore loads in the contraint inc files. If however the function has already been called once on the page load, the static var is already set and is returned without loading in the constrain inc files. Using global var is overkill especially since we're loading in inc files on 99% of pages that have no need for them.
Comment #4
deekayen commentedI'm with you on getting it out of hook_init(), you don't have to justify that part.
What I'm saying is that if you're only using the return value of your new function in admin.inc, then it can load the constraint files then. They don't need to be pre-loaded and set in a static var.
Comment #5
john.money commentedThere is a negligible performance hit for the array of drupal_substr($file->name, 11) which as you rightly point out is effectively discarded for all but the admin.inc. The benefit is that the file load logic is consistent throughout. And of course net-net, its a huge performance improvement over loading them for every page.
Function with static var seems a pretty common approach anywhere file io is used.
Comment #6
deekayen commentedI'm still not sure why you have all the extra calls. If that function doesn't include the relevant constraint files at the time you called it there, will stuff break?
Comment #7
deekayen commentedComment #8
tacituseu commentedthere's some leftover in password_policy.admin.inc:
global $_password_policy;Comment #9
deekayen commentedGood catch. As you probably noticed, your patch didn't apply anymore, so I tried to apply it the hard way...
Comment #10
afreeman commentedFatal error: Call to undefined function _password_policy_constraints() in modules/password_policy/password_policy.admin.inc on line 256
Comment #11
deekayen commentedPatch reverted.
Comment #12
tacituseu commentedSeems OP spelled it as 'contraints' (no s).
Comment #13
deekayen commentedspelling fixed. hope this thing is finally dead.
Comment #14
roball commentedDoes this mean the critical bug went into alpha4?
Comment #15
deekayen commentedYes. Download the latest nightly snapshot for the fix.
Comment #16
roball commentedSo correcting the latest affected version. I'll wait for the next release instead.
Comment #17
john.money commentedSorry for function name misspelling... my bad. :(
Comment #18
ryanscottdavis commentedBug is still active.
When I (admin) navigate to admin/settings/password_policy/add, I get the following error:
Fatal error: Call to undefined function _password_policy_constraints() in sites\all\modules\password_policy\password_policy.admin.inc on line 257
I understand the misspelling issue. Can we push this patch to the latest downloadable version? A recent download still gets "foreach (_password_policy_constraints() as $constraint) {" on line 257.
Comment #19
deekayen commentedYou guys are persistent... see 6.x-1.0-beta1