Example that shows the effect of scream
Introduction
Affecting PHP's Behaviour
PHP Manual

# Weak References

Table of Contents

- Introduction
- Installing/Configuring
  - Requirements
  - Installation
  - Resource Types
- WeakRef — The WeakRef class
  - Weakref::acquire — Acquires a strong reference on that object
  - Weakref::__construct — Constructs a new weak reference
  - Weakref::get — Returns the object pointed to by the weak reference
  - Weakref::release — Releases a previously acquired reference
  - Weakref::valid — Checks whether the object referenced still exists
- WeakMap — The WeakMap class
  - WeakMap::__construct — Constructs a new map
  - WeakMap::count — Counts the number of live entries in the map
  - WeakMap::current — Returns the current value under iteration
  - WeakMap::key — Returns the current key under iteration.
  - WeakMap::next — Advances to the next map element
  - WeakMap::offsetExists — Checks whether a certain object is in the map
  - WeakMap::offsetGet — Returns the value pointed to by a certain object
  - WeakMap::offsetSet — Updates the map with a new key-value pair
  - WeakMap::offsetUnset — Removes an entry from the map
  - WeakMap::rewind — Rewinds the iterator to the beginning of the map
  - WeakMap::valid — Returns whether the iterator is still on a valid map element

# The WeakRef class

## Introduction

The WeakRef class provides a gateway to objects without preventing the garbage collector from freeing those objects. It also provides a way to turn a weak reference into a strong one.

## Class synopsis

WeakRef
class WeakRef {
/* Methods */
public Weakref::__construct ([ object `$object` ] )
public bool Weakref::acquire ( void )
public object Weakref::get ( void )
public bool Weakref::release ( void )
public bool Weakref::valid ( void )
}

## Examples

Example #1 WeakRef usage example

`<?php
class MyClass {
public function __destruct() {
echo "Destroying object!\n";
}
}
$o1 = new MyClass;
$r1 = new WeakRef($o1);
if ($r1->valid()) {
echo "Object still exists!\n";
var_dump($r1->get());
} else {
echo "Object is dead!\n";
}
unset($o1);
if ($r1->valid()) {
echo "Object still exists!\n";
var_dump($r1->get());
} else {
echo "Object is dead!\n";
}
?>`

The above example will output:

    Object still exists!
    object(MyClass)#1 (0) {
    }
    Destroying object!
    Object is dead!

# The WeakMap class

## Introduction

## Class synopsis

WeakMap
class WeakMap implements Countable , ArrayAccess , Iterator {
/* Methods */
public __construct ( void )
public int count ( void )
public mixed current ( void )
public object key ( void )
public void next ( void )
public bool offsetExists ( object `$object` )
public mixed offsetGet ( object `$object` )
public void offsetSet ( object `$object` , mixed `$value` )
public void offsetUnset ( object `$object` )
public void rewind ( void )
public bool valid ( void )
}

## Examples

Example #1 Weakmap usage example

`<?php
$wm = new WeakMap();
$o = new StdClass;
class A {
public function __destruct() {
echo "Dead!\n";
}
}
$wm[$o] = new A;
var_dump(count($wm));
echo "Unsetting..\n";
unset($o);
echo "Done\n";
var_dump(count($wm));`

The above example will output:

    int(1)
    Unsetting..
    Dead!
    Done
    int(0)