How to Generate Multiple Unique License Keys at Once Using SunLicense

Pass the count parameter to the SunLicense constructor to batch-generate any number of license keys, which returns an array when the count exceeds one or a string for a single key.

The msbatal/php-license-key-generator repository provides a lightweight, self-contained PHP utility for creating product license keys. By leveraging the SunLicense class constructor parameters, you can generate multiple unique license keys at once without instantiating the class multiple times or writing custom loops.

Configuring the Constructor for Batch Generation

The SunLicense class accepts a $count parameter in its constructor (defined in [SunLicense.php](https://github.com/msbatal/php-license-key-generator/blob/main/SunLicense.php) at lines 56–71) that dictates how many keys to produce in a single operation. This value is stored internally in the private $keyCount property alongside other configuration options like $prefix, $template, and $case.

When you instantiate the class, set the count argument to your desired batch size:

$generator = new SunLicense(
    prefix: 'PRO',
    template: null,  // Uses default 'X9XX99-XX99-9X9X-99XX9X'
    case: 'upper',
    count: 10       // Generates 10 keys
);

The constructor signature stores these values for later use by the generation engine, making the class stateful for the specific batch operation.

The generate() Method and Return Type Logic

The generate() method (lines 20–33 in SunLicense.php) handles the actual batch creation. It implements a while loop that repeatedly calls the private license() method until the internal $keys array reaches the size specified by $keyCount.

Return type behavior:

  • If $keyCount equals 1, the method returns a single string.
  • If $keyCount is greater than 1, it returns an array of strings.

This adaptive return type eliminates the need for manual array handling when you only need one key, while still supporting bulk operations efficiently.

Template Parsing in the license() Method

Behind the scenes, the private license() method (lines 79–96 in SunLicense.php) constructs each individual key by walking through the template string character by character:

  • X or any alphabetic character → Random letter (uppercase or lowercase based on the $case setting)
  • 9 or any digit → Random digit 0–9
  • Any other character → A literal dash (-)

The optional $prefix is prepended to the templated segment before being added to the results array. Because generate() calls license() repeatedly in its internal loop, each key in a batch receives independent randomization.

Practical Batch Generation Example

The following example demonstrates how to generate multiple unique license keys at once using the count parameter:

<?php
require_once 'SunLicense.php';

// Configure for 5 keys
$generator = new SunLicense(
    prefix: 'PRO',
    template: null,
    case: 'upper',
    count: 5
);

$keys = $generator->generate();  // Returns array of 5 strings

foreach ($keys as $licenseKey) {
    echo $licenseKey . PHP_EOL;
}

For single-key generation, omit the count parameter or set it to 1:

$singleKey = (new SunLicense())->generate();  // Returns string directly
echo $singleKey;

Summary

  • Set the batch size using the count parameter in the SunLicense constructor (lines 56–71).
  • Retrieve multiple keys by calling generate(), which returns an array when count exceeds 1 (lines 20–33).
  • Template parsing occurs in the private license() method, using X for letters, 9 for digits, and other characters as separators (lines 79–96).
  • Adaptive return types mean you get a string for single keys and an array for batches, simplifying downstream code.

Frequently Asked Questions

What parameter controls how many keys SunLicense generates at once?

The count parameter (stored internally as $keyCount) passed to the class constructor determines the batch size. According to the source code in SunLicense.php lines 56–71, this value drives the while loop inside the generate() method.

Does SunLicense return an array or string when generating multiple keys?

When the count value is greater than 1, the generate() method returns an array of license key strings. If count is 1 (or omitted), it returns a single string directly. This logic is implemented in lines 20–33 of SunLicense.php.

Can I use a custom template when generating batches of keys?

Yes. The template parameter accepts any string where X represents a random letter, 9 represents a random digit, and other characters become separators. You can pass a custom template to the constructor alongside the count parameter, and the license() method (lines 79–96) will apply this pattern to every key in the batch.

Where is the batch generation logic implemented in the source code?

The batch generation logic resides in the generate() method at lines 20–33 of SunLicense.php. This method contains a while loop that continues calling the private license() method until the internal $keys array contains the number of entries specified by $keyCount.

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:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →