How to Access Generated License Keys Using the SunLicense Class
The SunLicense class stores every generated license key in its public $keys array property while also returning values directly from the generate() method, giving you two distinct ways to access your PHP license keys.
The msbatal/php-license-key-generator repository provides a lightweight, template-driven solution for creating product license keys in PHP. When you instantiate the SunLicense class and invoke its generation methods, the library maintains an internal record of every key created during that session. Understanding how to retrieve these values—whether from method returns or object properties—is essential for integrating the generator into your licensing workflow.
Understanding the SunLicense Key Storage Mechanism
The SunLicense class implements a dual-access pattern for retrieving generated keys. According to the source code in SunLicense.php, the class defines a public $keys property at line 49 that acts as a running accumulator. Simultaneously, the generate() method at line 121 implements logic that returns the key data to the caller.
When you call generate(), the method appends each newly created key to $this->keys. If you request a single key (the default behavior), the method returns that key as a string. If you request multiple keys via the constructor's $count parameter, the method returns the array of generated keys. In both cases, the public $keys property contains the same data, allowing you to access the results even if you did not capture the return value.
Methods to Access Generated License Keys
Retrieving Keys from the generate() Return Value
The most direct approach captures the return value of the generate() method. This method signature returns a string for single-key generation or an array for batch generation.
<?php
require 'SunLicense.php';
// Single key returns a string
$license = new SunLicense();
$key = $license->generate(); // Returns: "AB3-CD9-EF2"
echo "Generated: $key\n";
// Multiple keys returns an array
$batch = new SunLicense(null, null, 'upper', 5);
$keys = $batch->generate(); // Returns: ["XX1-XX2-XX3", "YY4-YY5-YY6", ...]
print_r($keys);
?>
Accessing Keys via the Public $keys Property
Because $keys is declared as public in SunLicense.php, you can read it directly from the instance after generation completes. This is useful for logging, debugging, or when you need to access the generation history without storing the return value.
<?php
require 'SunLicense.php';
$generator = new SunLicense('MYAPP', 'xx-99-XX', 'upper', 3);
$generator->generate();
// Access the internal $keys property directly
echo "Total keys generated: " . count($generator->keys) . "\n";
foreach ($generator->keys as $index => $licenseKey) {
echo "Key " . ($index + 1) . ": $licenseKey\n";
}
// The property persists the full history of this instance
var_dump($generator->keys);
?>
Practical Implementation Examples
Example 1: Single License Key with Custom Prefix
This example demonstrates generating a single key and accessing it through both the return value and the $keys property.
<?php
require 'SunLicense.php';
$single = new SunLicense('PROD'); // Prefix: PROD, default template
$key = $single->generate();
echo "Returned value: $key\n";
echo "From property: " . $single->keys[0] . "\n";
?>
Example 2: Batch Generation with Custom Template
When generating multiple keys with a specific pattern, the $keys property provides a convenient way to audit the complete batch.
<?php
require 'SunLicense.php';
// Template: 2 letters, 2 numbers, 2 uppercase letters
$custom = new SunLicense(null, 'xx-99-XX', 'upper', 10);
$batch = $custom->generate();
echo "Generated " . count($batch) . " keys\n";
// Verify via property
if (count($custom->keys) === 10) {
echo "All keys successfully stored in \$keys property\n";
}
?>
Example 3: Logging and Debugging Access
Direct property access enables you to implement logging without modifying the class internals.
<?php
require 'SunLicense.php';
class LicenseLogger {
public function generateAndLog($prefix, $template, $case, $count) {
$gen = new SunLicense($prefix, $template, $case, $count);
$gen->generate();
// Access public $keys for logging
foreach ($gen->keys as $key) {
error_log("License generated: $key");
}
return $gen->keys;
}
}
$logger = new LicenseLogger();
$keys = $logger->generateAndLog('APP', 'xx-99-XX', 'upper', 5);
?>
Summary
- The
SunLicenseclass inmsbatal/php-license-key-generatorprovides two ways to access generated license keys: the return value ofgenerate()and the public$keysproperty. - The
generate()method returns a string for single-key requests and an array for multiple keys, as implemented inSunLicense.phpat line 121. - The public
$keysproperty (defined at line 49 inSunLicense.php) accumulates all generated keys during the instance lifetime, enabling direct access for logging, debugging, or batch processing. - Both access methods contain identical data, allowing you to choose the approach that best fits your application's architecture.
Frequently Asked Questions
What happens to previously generated keys when I call generate() again?
The generate() method appends each new key to the existing $keys array rather than resetting it. According to the implementation in SunLicense.php, the property persists across multiple generate() calls on the same instance, allowing you to accumulate keys throughout the object's lifecycle. If you need to start fresh, you must instantiate a new SunLicense object or manually reset the $keys property.
Can I access license keys after the SunLicense object is destroyed?
No. Because the $keys property is a non-static instance variable, it exists only in memory for the duration of the object's lifetime. Once the SunLicense instance is destroyed or goes out of scope, the $keys array is garbage collected. To persist keys beyond the current request, you must store the return value of generate() or the contents of $keys in a database, file, or session variable before the object is destroyed.
Is the $keys property safe to use in multi-threaded environments?
PHP typically runs in a shared-nothing architecture where each request operates in its own process or thread, so concurrent access to a single SunLicense instance is uncommon. However, if you implement the class in a long-running process or multi-threaded context (such as with pthreads or parallel extensions), the public $keys property lacks synchronization mechanisms. You should implement your own locking strategy or use the return value of generate() rather than relying on the shared $keys property in concurrent scenarios.
How do I clear the $keys array between generation batches?
The SunLicense class does not provide a built-in method to reset the $keys property. To clear the array between batches, you can either re-instantiate the class with new SunLicense() or manually empty the property by assigning an empty array: $license->keys = []. The latter approach preserves your current configuration (prefix, template, case settings) while clearing the generation history, making it efficient for batch processing workflows where you want to reuse the same configuration parameters.
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 →