DWS WP Framework
  • Welcome
  • Primary goals
    • Modular design
    • No 3rd-party dependencies
  • Key concepts and dev tools
    • PHP and WP requirements
    • Object-Oriented Programming
    • Semantic Versioning
    • Version Control (git / GitHub)
    • Dependency Management (Composer)
    • Automated Testing (Codeception + Github Actions)
    • Dependency Injection (PHP-DI)
    • Coding Standards (PHPCS and PHPMD)
    • Dependencies Scoping (PHP-Scoper)
    • TypeScript and Sass
    • Task Runners (Grunt)
  • Setting up your dev environment
    • Windows
  • Your first plugin
    • Multiple plugins using the framework on the same site
  • Frequently Asked Questions
  • Bootstrapper Module
    • Motivation
    • How it works
    • How to use
    • White Labeling
  • Helpers Module
    • Motivation
    • How to use
  • Foundations Module
    • Motivation and How to use
    • Actions
      • Local action traits
      • Extension action traits
      • Integration action traits
    • States
    • Utilities
      • Stores
      • Handlers and Services
        • Logging Service
  • Plugin
    • Main Plugin Instance
    • Plugin Components
  • Hierarchies
  • Helpers
  • Utilities Module
    • Motivation and How to use
    • Hooks Service
      • Scoped Handler
    • Shortcodes Service
    • Templating Service
    • Assets Service
      • Scripts Handler
      • Styles Handler
    • CRON Events Service
      • Action Scheduler Handler
    • Admin Notices Service
    • Dependencies Service
    • Validation Service
  • Core Module
    • Motivation and How to use
    • Plugin Tree
      • Plugin Root
      • Plugin Functionality
    • Plugin Components
      • Internationalization
      • Installation / Upgrade / Uninstallation
  • Settings Module
    • Motivation and How to use
    • Settings Service
      • WordPress Handler
      • MetaBox Handler
      • ACF Handler
    • Validated Settings
  • WooCommerce Module
    • Motivation and How to use
    • Extended WC Logger
    • WC Settings Handler
Powered by GitBook
On this page

Was this helpful?

  1. Key concepts and dev tools

Dependency Management (Composer)

PreviousVersion Control (git / GitHub)NextAutomated Testing (Codeception + Github Actions)

Last updated 4 years ago

Was this helpful?

In short, is a dependency manager for PHP. If you’ve worked with npm for NodeJS, Maven for Java or NuGet for C#, the principle should be familiar. Basically, Composer lets us define a composer.json file in our project root which contains a list of dependencies for the project. Using commands such as composer install we can then automagically add those dependencies to our project.

By default, Composer pulls the dependencies from which is also where you can find the of our framework. You can also define your own package sources, such as GitHub or a private packages repository. You can read more about how that works and how a composer.json file looks like by reading of the official documentation.

Composer also has another ground-breaking advantage — it generates an autoloader for your project. You probably know this already, but in PHP you need to call include or require before using the contents of a file. It can become tedious (and it’s definitely error-prone) to do that for all the files in your project, especially as your plugin grows. Moreover, if you want to only load classes conditionally, that becomes a nightmare!

An lets you define a function that loads PHP files containing classes only when the class is used. That basically guarantees the conditional loading. Moreover, the Composer autoloader also supports loading simple files containing plain PHP functions (albeit non-conditionally).

By using the PSR-4 autoloading standard for our project structure and Composer for loading dependencies and generating an autoloader, it’s virtually impossible to cause a “function/class does not exist” runtime error in production!

of a composer.json file that is actually used by the first module of the framework, the Bootstrapper. The relevant entries are “autoload” and “autoload-dev”. The autoloader is then the only file that we need to require as one of the first things the module does in .

Composer
Packagist
open-source modules
this article
autoloader
Here is an example
bootstrap.php