admin管理员组文章数量:1130349
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
1 Answer
Reset to default 0Composer Autoloader as suggested in the comments is the best way to do it. Just use composers classmap feature:
"autoload": {
"classmap": ["classes/"]
}
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
1 Answer
Reset to default 0Composer Autoloader as suggested in the comments is the best way to do it. Just use composers classmap feature:
"autoload": {
"classmap": ["classes/"]
}
本文标签: phpAutoloading Classes in Plugins
版权声明:本文标题:php - Autoloading Classes in Plugins 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749181767a2328730.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论