By profix898 on
To reduce memory usage I split up my module into the actual .module file and several .inc files. Some others modules e.g. gallery do the
same. Trying to port a module to 5.0 I realised this is no longer possible and I cant explain why. I'm not even convinced this is a Drupal issue.
Code like this works fine in 4.7 and CVS:
in mymodule.module
function mymodule_admin() {
return _mymodule_admin();
}
function _mymodule_admin() {
return 'test';
}
Code like this works fine in 4.7 but NOT with CVS (on same server)
in mymodule.module
function mymodule_admin() {
require_once (drupal_get_path('module', 'mymodule').'/mymodule_admin.inc');
return _mymodule_admin();
}
in mymodule_admin.inc
function _mymodule_admin() {
return 'test';
}
Where require_once is called the whole .inc file seems to be processed and return _mymodule_admin(); is reported to be undefined.
Can anyone confirm this behaviour?
Any thoughts? Any ideas?
Comments
PHP scoping
including a file when inside a function (or an eval) will sometimes only load the included functions and 'globals' into the local scope.
PHP5 will let you nest function declarations ... if you are so inclined. Useful for OO, but confusing when working with a bunch of arbitrary libraries like Drupal modules.
Plus, is certain conditions - like php-code-nodes - the php is eval-ed, leading to scary contexts. It's concievable (but I don't THINK it happens) that the admin/modules page may try to eval some things rather than just execute them. This would produce the different results you seem to be getting.
ANYWAY. Compare with running the same code with the include outside of the function. Yeah, I know sometimes conditional includes are better. There may be ways around it, not really sure right now.
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
.dan. is the New Zealand Drupal Developer working on Government Web Standards