# How to Add a Prefix to Generated License Keys with SunLicense

> Learn to add a prefix to generated license keys using SunLicense. Easily prepend custom text to your license keys by passing a string to the constructor for enhanced organization and tracking.

- Repository: [Mehmet Selcuk Batal/php-license-key-generator](https://github.com/msbatal/php-license-key-generator)
- Tags: how-to-guide
- Published: 2026-03-07

---

**Pass a string to the `SunLicense` constructor to automatically prepend a custom prefix followed by a hyphen to every generated license key.**

The `SunLicense` class in the `msbatal/php-license-key-generator` repository provides a straightforward mechanism to add a prefix to generated license keys with SunLicense through its constructor-based configuration. By initializing the class with a specific prefix string, developers can brand their license keys while maintaining the library's template-driven generation logic.

## Understanding the SunLicense Prefix Architecture

### The $prefix Property

The prefix functionality centers on a **private `$prefix` property** defined in [`SunLicense.php`](https://github.com/msbatal/php-license-key-generator/blob/main/SunLicense.php). This property stores the optional string that appears before the generated key segments. When populated, it serves as the root identifier for your application or product line.

### Constructor Initialization

Prefix assignment occurs during object instantiation. In `SunLicense::__construct()` (lines 56–62), the constructor receives the `$prefix` parameter and assigns it to `$this->prefix` if the value is not empty. This design ensures that the prefix is immutable for the lifetime of the generator instance, providing consistent key formatting across multiple generation calls.

## How the Prefix is Applied During Key Generation

### The license() Method Logic

The actual prefix injection happens inside `SunLicense::license()` (lines 79–84). When `$this->prefix` is set, the method concatenates the prefix followed by a hyphen character (`'-'`) to the `$key` variable before processing the template. This hardcoded separator ensures visual distinction between the product identifier and the random key segments.

### Template Processing After Prefix

Following the prefix attachment (lines 84–95), the method iterates through the `$template` string. It substitutes `X` characters with random letters and `9` characters with random digits, appending these to the prefixed string. Because the prefix is prepended as a discrete block, the template defines only the variable portion of the key, keeping the branding element separate from the entropy source.

## Practical Code Examples

### Generate a Single Key with a Custom Prefix

To create a branded license key, instantiate `SunLicense` with your product identifier as the first argument:

```php
require_once 'SunLicense.php';

// Initialize with "MYAPP" prefix, default template, uppercase letters
$lic = new SunLicense('MYAPP');
echo $lic->generate();   // Output: MYAPP-K7FQ84-AB12-7K9Q-48LM7P

```

### Generate Multiple Keys with Prefix and Custom Template

Combine prefix branding with custom templates and batch generation:

```php
require_once 'SunLicense.php';

$prefix   = 'PROD';
$template = 'XX99-9X9X';
$count    = 5;

$lic = new SunLicense($prefix, $template, 'upper', $count);
$keys = $lic->generate();   // Returns array of 5 keys
print_r($keys);
// Output: [PROD-AB12-3C4D, PROD-EF56-7G8H, ...]

```

### Omitting the Prefix for Default Behavior

If you do not require branding, omit the prefix argument to generate keys matching the template exactly:

```php
$lic = new SunLicense();   // No prefix argument
echo $lic->generate();     // Output: X7QG84-AB12-7K9Q-48LM7P

```

## Key Implementation Files

The prefix functionality is implemented across the following files in the `msbatal/php-license-key-generator` repository:

- **[`SunLicense.php`](https://github.com/msbatal/php-license-key-generator/blob/main/SunLicense.php)** – Defines the `SunLicense` class, including the `$prefix` property, constructor logic (lines 56–62), and the `license()` method (lines 79–95) that handles prefix concatenation.
- **[`test/SunLicense.php`](https://github.com/msbatal/php-license-key-generator/blob/main/test/SunLicense.php)** – Contains example usage patterns and a simple test harness demonstrating various prefix configurations.
- **[`test/index.php`](https://github.com/msbatal/php-license-key-generator/blob/main/test/index.php)** – Entry point for manual testing, showing instantiation examples with and without prefixes.

## Summary

- **Prefix configuration** occurs via the `SunLicense` constructor, which stores the value in a private `$prefix` property.
- **Automatic formatting** prepends the prefix followed by a hyphen during the `license()` method execution, separating the brand identifier from the random key segments.
- **Template independence** ensures that the prefix does not interfere with the `X` and `9` substitution logic, maintaining consistent key entropy regardless of branding.
- **Optional implementation** allows developers to omit the prefix entirely, generating keys that match the template pattern without additional identifiers.

## Frequently Asked Questions

### Can I add a prefix to existing keys without regenerating them?

No, the `SunLicense` class applies prefixes during the generation phase within the `license()` method. To add prefixes to existing keys, you would need to manually concatenate the prefix and hyphen to your stored keys or regenerate them using the constructor-based prefix configuration.

### What happens if I pass an empty string as the prefix?

The constructor checks if the prefix parameter is not empty before assigning it to `$this->prefix` (lines 60–62 in [`SunLicense.php`](https://github.com/msbatal/php-license-key-generator/blob/main/SunLicense.php)). Passing an empty string results in the prefix remaining unset, causing the `license()` method to skip the prefix concatenation step and generate keys matching the template exactly.

### Is there a maximum length limit for the prefix?

The source code does not enforce a maximum length limit on the `$prefix` property. However, practical considerations for database storage and key readability suggest keeping prefixes under 10–15 characters. The hyphen separator is hardcoded, so extremely long prefixes will increase total key length linearly.

### Can I change the separator between prefix and key?

The hyphen separator is hardcoded in the `license()` method at line 81: `$key = $this->prefix . '-';`. To use a different separator, you would need to modify the source code in [`SunLicense.php`](https://github.com/msbatal/php-license-key-generator/blob/main/SunLicense.php) or extend the class to override the `license()` method with your preferred delimiter logic.