W3cubDocs

/PHP

Weakref::acquire

(PECL weakref >= 0.1.0)

Weakref::acquireAcquires a strong reference on that object

Description

public Weakref::acquire ( ) : bool

Acquires a strong reference on that object, virtually turning the weak reference into a strong one.

The Weakref instance maintains an internal acquired counter to track outstanding strong references. If the call to Weakref::acquire() is successful, this counter will be incremented by one.

Parameters

This function has no parameters.

Return Values

Returns true if the reference was valid and could be turned into a strong reference, false otherwise.

Examples

Example #1 Weakref::acquire() example

<?php
class MyClass {
    public function __destruct() {
        echo "Destroying object!\n";
    }
}

$o1 = new MyClass;

$r1 = new Weakref($o1);

$r1->acquire();

echo "Unsetting o1...\n";
unset($o1);

$o2 = $r1->get();

$r1->release();

echo "Unsetting o2...\n";
unset($o2);
?>

The above example will output:

Unsetting o1...
Unsetting o2...
Destroying object!

Example #2 Nested acquire/release example

<?php
class MyClass {
    public function __destruct() {
        echo "Destroying object!\n";
    }
}

$o1 = new MyClass;

$r1 = new Weakref($o1);

echo "Acquiring...\n";
$r1->acquire();

echo "  Unsetting...\n";
unset($o1);

echo "  Acquiring...\n";
$r1->acquire();

echo "    Acquiring...\n";
$r1->acquire();

echo "    Releasing...\n";
$r1->release();

echo "  Releasing...\n";
$r1->release();

echo "Releasing...\n";
$r1->release();

?>

The above example will output:

Acquiring...
  Unsetting...
  Acquiring...
    Acquiring...
    Releasing...
  Releasing...
Releasing...
Destroying object!

See Also

© 1997–2020 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.
https://www.php.net/manual/en/weakref.acquire.php