How to Install the PHP License Key Generator Library (SunLicense)
The PHP License Key Generator (SunLicense) installs via a single file drop-in that requires only PHP 5.5+ and no external dependencies.
The msbatal/php-license-key-generator repository provides a lightweight, self-contained solution for creating formatted license keys in PHP. Since the library consists of a single class file without external dependencies, you can install the PHP License Key Generator library by simply including it in your project. This guide walks through the exact steps to download, require, and instantiate the SunLicense class based on the source code implementation.
Installation Steps for the PHP License Key Generator
Download the SunLicense.php Class File
The library ships as a single class definition located in SunLicense.php at the repository root. Download or clone the msbatal/php-license-key-generator repository and place this file anywhere within your project's include path. The class requires no additional vendor folders or autoloaders, making it ideal for legacy projects or minimal PHP environments.
Require the Class in Your Project
Load the class using PHP's native require_once statement or your preferred autoloader. The repository's README.md explicitly documents this requirement:
require_once 'SunLicense.php';
Place this line at the top of any script that needs to generate license keys. If you place SunLicense.php in a subdirectory (such as lib/ or vendor/), adjust the path accordingly (e.g., require_once 'lib/SunLicense.php').
Instantiate the Generator
Create a new instance of the SunLicense class to begin generating keys. The constructor accepts four optional parameters that control output formatting and quantity:
$prefix– String prepended to every key (e.g.,SLK-)$template– Pattern usingAfor letters,9for digits, and-for separators$case– Either'upper'(default) or'lower'for alphabetic characters$count– Integer specifying how many unique keys to generate (defaults to1)
Internally, the constructor stores these values in private properties ($prefix, $template, $case, $keyCount) at lines 56–72 of SunLicense.php, applying sensible defaults when arguments are omitted.
Understanding the SunLicense Constructor Parameters
The SunLicense class flexibility comes from its template-driven generation system. When you install the PHP License Key Generator library and instantiate the class, you can customize:
Template Characters:
Agenerates a random alphabetic character9generates a random numeric digit-or other literals pass through unchanged
Case Sensitivity:
Pass 'lower' as the third constructor argument to output lowercase letters instead of the default uppercase.
Batch Generation:
Set the fourth parameter to any integer greater than 1 to receive an array of unique keys rather than a single string.
Code Examples: Installing and Using SunLicense
Basic Single-Key Generation
This minimal example shows the default configuration after you install the PHP License Key Generator library:
require_once 'SunLicense.php';
$license = new SunLicense(); // default settings: no prefix, template "AAAA-AAAA-AAAA-AAAA", upper case, count 1
echo $license->generate(); // prints a single license key like "X7K9-P2M5-V8N3-Q4W6"
Custom Configuration with Multiple Keys
Generate five keys with a custom prefix, specific template, and lowercase letters:
require_once 'SunLicense.php';
// prefix = "SLK", template = "AA99-9A9A-A9A9-99AA", case = lower, generate 5 keys
$license = new SunLicense('SLK', 'AA99-9A9A-A9A9-99AA', 'lower', 5);
$keys = $license->generate(); // returns an array of 5 unique keys
foreach ($keys as $key) {
echo $key . PHP_EOL;
}
Generating Batches Using Defaults
To generate multiple keys while keeping default formatting, pass null for unused parameters:
require_once 'SunLicense.php';
// No prefix/template/case, just 3 keys
$license = new SunLicense(null, null, null, 3);
$keys = $license->generate(); // array with 3 keys using default template
print_r($keys);
Key Files in the Repository
Understanding the file structure helps verify your installation:
SunLicense.php– Core class implementing key generation, uniqueness checking via the privatecheck()method, and the publicgenerate()entry point (lines 21–133).README.md– Official installation guide and parameter reference.test/index.php– Optional verification script demonstrating basic usage.test/SunLicense.php– Extended test cases showing various constructor configurations.
Summary
- Single-file installation: Copy
SunLicense.phpfrom themsbatal/php-license-key-generatorrepository to your project. - Zero dependencies: Requires only PHP 5.5+; no Composer or external libraries needed.
- Simple inclusion: Use
require_once 'SunLicense.php'to load the class. - Flexible instantiation: Configure via four optional constructor parameters controlling prefix, template pattern, letter case, and key count.
- Internal validation: The
license()andcheck()methods ensure uniqueness before returning results viagenerate().
Frequently Asked Questions
Does the PHP License Key Generator require Composer?
No. According to the source code in SunLicense.php, the library is a single self-contained class with no external dependencies. You can install it by directly downloading the file and using require_once, bypassing Composer entirely.
What PHP version is required for SunLicense?
The library requires PHP 5.5 or higher. This is the only system requirement; there are no extensions or additional runtime dependencies needed to install the PHP License Key Generator library.
How does SunLicense ensure unique keys?
The class maintains a private $keys array property. When generating multiple keys, the private check() method compares each newly generated candidate against this array. If a duplicate is detected, the license() method regenerates that segment until uniqueness is confirmed before the public generate() method returns the final set.
Can I customize the license key format?
Yes. Pass a custom template string as the second constructor argument using A for alphabetic characters, 9 for digits, and literal characters like - for separators. For example, AA99-9999 produces keys like "XP47-8291". Combine this with the prefix and case parameters to match your specific formatting requirements.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →