How to invoke a PSR-4 PHP package in a custom Moodle plugin?

How to invoke a PSR-4 PHP package in a custom Moodle plugin?

by wz z -
Number of replies: 2

I want to invoke this PSR-4 PHP package, which is recommended to be installed by composer, because it has many dependencies and an autoload.php file. I've no idea to do call it in a custom Moodle plugin? Are there any examples?

Average of ratings: -
In reply to wz z

Re: How to invoke a PSR-4 PHP package in a custom Moodle plugin?

by Juho Jaakkola -

Go to the directory that contains your plugin (for example mod/wechat/) and give the command:

composer require overtrue/wechat

It will automatically create the following files and directories:

  • composer.json
  • composer.lock
  • vendor/
The vendor directory will contain a file called autoload.php. Include that file in your plugin code, and you can start using the API provided by the package.


For example in mod/wechat/view.php:

<?php
require_once('vendor/autoload.php'); require_once('../../config.php');
// You can now access the API provided by the package:
$foo = new \Some\Example\Class($some, $parameters); $foo->doSomething();


In reply to Juho Jaakkola

Re: How to invoke a PSR-4 PHP package in a custom Moodle plugin?

by Juho Jaakkola -

(Oh, and in this context the correct term is "composer package" instead of "PSR-4 PHP package". PSR-4 simply refers to the way that the package is loading it's classes.)