PHP Luminova: Store a Value with the WeakReference Utility
Store, replace, or read a value through a reusable wrapper.
Luminova’s WeakReference is a mutable value holder. It keeps a regular PHP reference to a stored object for as long as that object remains in the wrapper. To release the stored value, replace it with null. PHP’s built-in \WeakReference provides garbage-collection-aware object references.
Usage
use Luminova\Utility\Reference\WeakReference;
$reference = new WeakReference('draft');
$reference->setValue('published');
echo $reference->getValue(); // published
$reference->setValue(null);Class Definition
- Full namespace:
Luminova\Utility\Reference\WeakReference - This class is final.
Methods
__construct
Create a holder with an optional initial value.
public function __construct(mixed $reference = null)Parameters:
| Parameter | Type | Description |
|---|---|---|
$reference | mixed | Initial value to store (default: null). |
setValue
Replace the stored value. Pass null to clear it.
public function setValue(mixed $reference): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$reference | mixed | New value to store. |
Return:
self - The same wrapper instance.
getValue
Read the current stored value.
public function getValue(): mixedReturn:
mixed - The stored value, which may be null.