|
|
(3 intermediate revisions by 2 users not shown) |
Line 1: |
Line 1: |
− | Module class autoloading enables the automatic loading of classes when stored in a prescribed way within WHMCS modules.
| + | <div class="docs-alert-info"> |
− | | + | <span class="title">Note</span><br /> |
− | It is provided as a tool that makes it easier for module developers to create helper classes that they can call on and load throughout the WHMCS product from modules, hooks and other custom integration code.
| + | This page has moved to [https://developers.whmcs.com/modules/module-class-autoloading/ https://developers.whmcs.com/modules/module-class-autoloading/] |
− | | + | </div> |
− | <div class="docs-alert-info">This is not intended to be a replacement for modern PHP development practices, (ie. use of Composer or more complex custom-made autoloaders).</div> | |
− | | |
− | ==Supported Module Types==
| |
− | | |
− | The following module types support autoloading.
| |
− | | |
− | * Addon
| |
− | * Fraud
| |
− | * Gateway
| |
− | * Registrar
| |
− | * Report
| |
− | * Server
| |
− | * Widget
| |
− | | |
− | ==Usage==
| |
− | | |
− | Locate your class file(s) in a sub-directory named 'lib' relative to the deployed module. For example:
| |
− | | |
− | /path/to/whmcs/modules/{ModuleType}/{ModuleName}/lib/{ClassName}.php
| |
− | | |
− | Add the module namespace to the top of your class file(s):
| |
− | | |
− | {source lang="php"}
| |
− | namespace WHMCS\Module\{ModuleType}\{ModuleName};
| |
− | {/source}
| |
− | | |
− | To invoke the class, simply add the use statement and invoke as normal:
| |
− | | |
− | <source lang="php">
| |
− | use WHMCS\Module\{ModuleType}\{ModuleName}\{ClassName};
| |
− | | |
− | $hello = new {ClassName}();
| |
− | </source> | |
− | | |
− | <div class="docs-alert-warning">For module types that do not have a directory per module by default (for example gateways and reports), you will need to create the directory and sub-directory 'lib' to utilise autoloading.</div>
| |
− | | |
− | ==Example Usage==
| |
− | | |
− | The following example demonstrates use of the module namespace autoloading for a class named 'HelloWorld' within the addon module 'Sample Provisioning Module':
| |
− | | |
− | Filename: '''/modules/addons/sample-provisioning-module/lib/HelloWorld.php'''
| |
− | <source lang="php">
| |
− | namespace WHMCS\Module\Addon\SampleProvisioningModule;
| |
− | | |
− | /** | |
− | * Hello World Class
| |
− | *
| |
− | * @copyright Copyright (c)
| |
− | * @license http://www.whmcs.com/license/ WHMCS Eula
| |
− | */
| |
− | class HelloWorld {
| |
− | | |
− | public function printHello()
| |
− | {
| |
− | print 'Hello World';
| |
− | }
| |
− | | |
− | }
| |
− | </source>
| |
− | | |
− | Filename: '''/modules/addons/sample-provisioning-module/sample-provisioning-module.php'''
| |
− | | |
− | <source lang="php">
| |
− | use WHMCS\Module\Addon\SampleProvisioningModule\HelloWorld;
| |
− | | |
− | $hello = new HelloWorld();
| |
− | $hello->printHello();
| |
− | </source> | |