The WeakReference class
[PHP 7 >= 7.4.0, PHP 8]
Introduction
Weak references allow the programmer to retain a reference to an object which does not prevent the object from being destroyed. They are useful for implementing cache like structures.
WeakReferences cannot be serialized.
Class synopsis
final class WeakReference {
public get[]: ?object
}
WeakReference Examples
Example #1 Basic WeakReference Usage
The above example will output something similar to:
object[stdClass]#1 [0] { } NULL
Table of Contents
- WeakReference::__construct — Constructor that disallows instantiation
- WeakReference::create — Create a new weak reference
- WeakReference::get — Get a weakly referenced Object
Sandor Toth ¶
2 years ago
You might consider to use WeakReference in your Container class. Don't forget to create the object into a variable and pass the variable to WeakReference::create[] otherwise you going to ->get[] null.
Consider as wrong solution, which returns null
Consider as GOOD solution, which returns App instance
Submit a Pull Request Report a Bug
[PHP 7 >= 7.4.0, PHP 8]
WeakReference::create — Create a new weak reference
Description
public static
WeakReference::create[object $object
]: WeakReference
Creates a new WeakReference.
Parameters
object
The object to be weakly referenced.
Return Values
Returns the freshly instantiated object.
+add a note
User Contributed Notes
There are no user contributed notes for this page.
Submit a Pull Request Report a Bug
[PHP 7 >= 7.4.0, PHP 8]
WeakReference::get — Get a weakly referenced Object
Description
public WeakReference::get[]: ?object
Gets a weakly referenced object. If the object has already been destroyed, null
is returned.
Parameters
This function has no parameters.
Return Values
Returns the referenced object, or null
if the object has been destroyed.
+add a note
User Contributed Notes
There are no user contributed notes for this page.