
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP WeakReference class
Introduction
With Weak references, it is possible to retain a reference to an object which does not prevent the object from being destroyed. Implementing cache like structures can be done by Weak reference.
A weak reference is similar to a normal reference, except that it doesn’t prevent the garbage collector from collecting the object. If strong references to that object are not found, it will be immediately removed frommemory. This way it is possible to implement most of the benefits of a cache, with no memory issues.
WeakReference class has been introduced in PHP 7.4. Before this version, same effect used to be obtained by weakref extension. The WeakReference class has following structure
Syntax
WeakReference { /* Methods */ public __construct ( void ) public static create ( object $referent ) : WeakReference public get ( void ) : ?object }
Methods
public WeakReference::__construct ( void ) — disallow instantiation of the WeakReference class. It can be instantiated with the factory method WeakReference::create().
public static WeakReference::create ( object $referent ) : WeakReference — Creates a new WeakReference.
public WeakReference::get ( void ) : ?object — Gets a weakly referenced object. If the object has already been destroyed, NULL is returned.
Generator Example
Following example has object of myclass. WeakReference to its object shows one reference. Object is then unset. Now WeakReference returns NULL
Example
<?php class myclass{ function Hello(){ echo "Hello"; } } $obj = new myclass(); $ref = WeakReference::create($obj); var_dump($ref->get()); unset($obj); var_dump($ref->get()); ?>
Output
Above program shows following output
object(myclass)#1 (0) { } NULL
- Related Articles
- PHP Class Abstraction
- PHP Class Constants
- PHP Class properties
- PHP Closure class
- PHP Generator class
- Explain abstract class in PHP.
- PHP Basics of Class and Object
- Can we define constant in class constructor PHP?
- PHP Inherit the properties of non-class object?
- Instance child class in abstract static method PHP?
- Explain final class and final method in PHP.
- How to call parent constructor in child class in PHP?
- Check whether property exists in object or class with PHP
- PHP php://
- Is there any advantage to using __construct() instead of the class's name for a constructor in PHP?
