I am porting a couple of custom modules I wrote for myself in Drupal 8. One is a Block.
I have included one class at the beginning of the module file
require_once 'libraries/SunMoonFunctions.php';
and similarly in the SunMoonFunctions class I have used 'require_once' for more classes.
I can clear the cache using Drush with no errors, but when I create an instance of the class
$now = strtotime(date("Y/m/d h:i:s", time())); $smfuncs = new SunMoonFunctions($now);
and load page with the block on it I get the following error: Class 'SunMoonFunctions' not found
I have looked through the API to see if there is a method to include a php library as you do with js, but cannot find one. Also I cannot find anything on the web about this.
I have tried using a namespace, but then the whole page will not load.
I have also included the file in my hook_autoload_info() function
function astro_stuff_autoload_info() { return array( 'AstroStuff' => 'astro_stuff.inc', 'SunMoonFunctions' => 'libraries/SunMoonFunctions.php', ); }
but I still get the same error.
Any ideas would be welcome.
In Backdrop, classes are included differently than in Drupal 7 or 8. You should not need either the require_once or module_load_include() calls, only the hook_autoload_info() function (https://api.backdropcms.org/api/backdrop/core%21modules%21system%21syste...). That is the way it is done for the Node, User, File, Comment, etc. classes in core.
I think you probably do not need
in astro_stuff_autoload_info().
Is there a class definition in astro_stuff.inc? If that is just a normal include file with regular functions, include it with module_load_include() and remove it from astro_stuff_autoload_info(). (Although it might not hurt to leave it there. Try it both ways.)
It's interesting that Nate did it the way he did in the GitHub API integration module.