php - Use composer to load custom classes

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Upd

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';
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745099748a4611202.html

相关推荐

  • php - Use composer to load custom classes

    Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Upd

    15小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信