$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Autoloading Classes in Plugins|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Autoloading Classes in Plugins

matteradmin8PV0评论

In a custom WordPress plugin I have a folder /classes with about 20 classes. Classes sometimes change, come and go. I want all those classes from the folder to be loaded automatically.

No my idea was to load those files by a simple loop require:

foreach (scandir(dirname(__FILE__)."/classes/") as $filename) {
    $path = dirname(__FILE__) . '/' . $filename;
    if (is_file($path)) {
        require $path;
    }
}

However this does not work because there are subclasses loaded before superclasses and I get a fatal error.

PHP usually solves this problem with the spl_autoload_register() function.

However this seems not to work if used in multiple plugins. Has anybody found a good solution to this problem yet?

In a custom WordPress plugin I have a folder /classes with about 20 classes. Classes sometimes change, come and go. I want all those classes from the folder to be loaded automatically.

No my idea was to load those files by a simple loop require:

foreach (scandir(dirname(__FILE__)."/classes/") as $filename) {
    $path = dirname(__FILE__) . '/' . $filename;
    if (is_file($path)) {
        require $path;
    }
}

However this does not work because there are subclasses loaded before superclasses and I get a fatal error.

PHP usually solves this problem with the spl_autoload_register() function.

However this seems not to work if used in multiple plugins. Has anybody found a good solution to this problem yet?

Share Improve this question edited Nov 14, 2018 at 16:54 Blackbam asked Nov 14, 2018 at 16:46 BlackbamBlackbam 57511 silver badges28 bronze badges 2
  • Have you considered using the composer autoloader? I have multiple plugins each with their own composer generated autoloaders, and they work just fine – Tom J Nowell Commented Nov 14, 2018 at 17:42
  • Not yet thats a good idea. – Blackbam Commented Nov 14, 2018 at 17:56
Add a comment  | 

1 Answer 1

Reset to default 0

Composer Autoloader as suggested in the comments is the best way to do it. Just use composers classmap feature:

"autoload": {
    "classmap": ["classes/"]
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far