最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Use composer to load custom classes

programmeradmin0浏览0评论
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 5 years ago.

Improve this question

I want to use composer to load my custom classes inside my plugin. I've tried to use it but without success. Is possible to use compose for the plugin develop to manage custom classes used inside it? Can anyone point me into the roght direction?

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 5 years ago.

Improve this question

I want to use composer to load my custom classes inside my plugin. I've tried to use it but without success. Is possible to use compose for the plugin develop to manage custom classes used inside it? Can anyone point me into the roght direction?

Share Improve this question asked Oct 7, 2019 at 17:10 userone2userone2 441 silver badge5 bronze badges 4
  • How exactly is your WordPress project been set up? From a Composer template? And now you want to autoload project dependencies in your plugin? Or does your plugin have a composer.json and you want to autoload dependencies there? Did googling for "wordpress autoload plugin" shed any lights on your issue? – norman.lol Commented Oct 7, 2019 at 17:25
  • Are you trying to set up and use an autoloader for your plugin? Or do you mean something else? – Tom J Nowell Commented Oct 7, 2019 at 17:35
  • @leymannx I want to use composer to load the plugin classes. for now I'm requiring classes using require_once, this is not the best option because the plugin will become more modular in the future. @TomJNowell Yes, the autoloader will be used only internally from the plugin – userone2 Commented Oct 7, 2019 at 17:38
  • 1 An autoloader and composer are not the same thing. Composer includes an autoloader, but is mainly a PHP package manager. If you want to autoload classes that belong to the plugin, and not a separate package, then you don't need composer. – Jacob Peattie Commented Oct 7, 2019 at 23:45
Add a comment  | 

1 Answer 1

Reset to default 1

Without more context from you, I can only assume and show you what I have done that works for me using PSR4 Autoloading.

Example:

Assuming that all my custom class directories and files is in ./inc folder

In your composer.json, add this

"autoload": {
    "psr-4": {
        "Inc\\": "./inc"  
    }
}

Inc is the vendor name of your application, use this for namspacing files inside your inc directory like namespace Inc/Api;

./inc is the directory(with all the class files or nested directory) you want to autoload.

Next, do this in your terminal to generate the vendor directory & autoload the files.

composer dump-autoload

Lastly, require the the autoloading by adding this to your plugin file eg. my-awesome-plugin.php

if (file_exists(dirname(__FILE__) . '/vendor/autoload.php')) {
    require_once dirname(__FILE__) . '/vendor/autoload.php';
}
发布评论

评论列表(0)

  1. 暂无评论