# How to Access Generated License Keys Using the SunLicense Class

> Learn how to access generated license keys with the SunLicense class. Discover two ways to retrieve your keys from the public $keys array or the generate method.

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

---

**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`](https://github.com/msbatal/php-license-key-generator/blob/main/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
<?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`](https://github.com/msbatal/php-license-key-generator/blob/main/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
<?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
<?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
<?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
<?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 `SunLicense` class in `msbatal/php-license-key-generator` provides two ways to access generated license keys: the return value of `generate()` and the public `$keys` property.
- The `generate()` method returns a **string** for single-key requests and an **array** for multiple keys, as implemented in [`SunLicense.php`](https://github.com/msbatal/php-license-key-generator/blob/main/SunLicense.php) at line 121.
- The **public `$keys` property** (defined at line 49 in [`SunLicense.php`](https://github.com/msbatal/php-license-key-generator/blob/main/SunLicense.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`](https://github.com/msbatal/php-license-key-generator/blob/main/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.