Weak References

目录

简介

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.

Note:

The class WeakRef is not to be confused with the class WeakReference.

类摘要

WeakRef

class WeakRef {

/* 方法 */

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 )

}

范例

示例 #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";
}
?>

以上例程会输出:

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

Weakref::acquire

Acquires a strong reference on that object

说明

public bool Weakref::acquire ( void )

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.

参数

此函数没有参数。

返回值

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

范例

示例 #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);
?>

以上例程会输出:

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

示例 #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();

?>

以上例程会输出:

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

参见

  • Weakref::release

Weakref::__construct

Constructs a new weak reference

说明

public Weakref::__construct ( object $object )

Constructs a new weak reference.

参数

object
The object to reference.

返回值

没有返回值。

范例

示例 #1 Weakref::__construct 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";
}
?>

以上例程会输出:

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

Weakref::get

Returns the object pointed to by the weak reference

说明

public object Weakref::get ( void )

Returns the object pointed to by the weak reference.

参数

此函数没有参数。

返回值

Returns the object if the reference is still valid, NULL otherwise.

参见

  • Weakref::valid

Weakref::release

Releases a previously acquired reference

说明

public bool Weakref::release ( void )

Releases a previously acquired reference, potentially turning a strong reference back into a weak reference.

The Weakref instance maintains an internal acquired counter to track outstanding strong references. If the call to Weakref::release is successful, this counter will be decremented by one. Once this counter reaches zero, the strong reference is turned back into a weak reference.

参数

此函数没有参数。

返回值

Returns TRUE if the reference was previously acquired and thus could be released, FALSE otherwise.

范例

示例 #1 Weakref::release 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);
?>

以上例程会输出:

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

参见

  • Weakref::acquire

Weakref::valid

Checks whether the object referenced still exists

说明

public bool Weakref::valid ( void )

Checks whether the object referenced still exists.

参数

此函数没有参数。

返回值

Returns TRUE if the object still exists and is thus still accessible via Weakref::get, FALSE otherwise.

参见

  • Weakref::get

简介

类摘要

WeakMap

class WeakMap implements Countable , ArrayAccess , Iterator {

/* 方法 */

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 )

}

范例

示例 #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));

以上例程会输出:

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

WeakMap::__construct

Constructs a new map

说明

public WeakMap::__construct ( void )

Constructs a new map.

参数

此函数没有参数。

返回值

没有返回值。

WeakMap::count

Counts the number of live entries in the map

说明

public int WeakMap::count ( void )

Counts the number of live entries in the map.

参数

此函数没有参数。

返回值

Returns the number of live entries in the map.

WeakMap::current

Returns the current value under iteration

说明

public mixed WeakMap::current ( void )

Returns the current value being iterated on in the map.

参数

此函数没有参数。

返回值

The value currently being iterated on.

WeakMap::key

Returns the current key under iteration

说明

public object WeakMap::key ( void )

Returns the object serving as key in the map, at the current iterating position.

参数

此函数没有参数。

返回值

The object key currently being iterated.

WeakMap::next

Advances to the next map element

说明

public void WeakMap::next ( void )

Advances to the next map element.

参数

此函数没有参数。

返回值

没有返回值。

WeakMap::offsetExists

Checks whether a certain object is in the map

说明

public bool WeakMap::offsetExists ( object $object )

Checks whether the passed object is referenced in the map.

参数

object
Object to check for.

返回值

Returns TRUE if the object is contained in the map, FALSE otherwise.

WeakMap::offsetGet

Returns the value pointed to by a certain object

说明

public mixed WeakMap::offsetGet ( object $object )

Returns the value pointed to by a certain object.

参数

object
Some object contained as key in the map.

返回值

Returns the value associated to the object passed as argument, NULL otherwise.

WeakMap::offsetSet

Updates the map with a new key-value pair

说明

public void WeakMap::offsetSet ( object $object , mixed $value )

Updates the map with a new key-value pair. If the key already existed in the map, the old value is replaced with the new.

参数

object
The object serving as key of the key-value pair.

value
The arbitrary data serving as value of the key-value pair.

返回值

没有返回值。

WeakMap::offsetUnset

Removes an entry from the map

说明

public void WeakMap::offsetUnset ( object $object )

Removes an entry from the map.

参数

object
The key object to remove from the map.

返回值

没有返回值。

WeakMap::rewind

Rewinds the iterator to the beginning of the map

说明

public void WeakMap::rewind ( void )

Rewinds the iterator to the beginning of the map.

参数

此函数没有参数。

返回值

没有返回值。

WeakMap::valid

Returns whether the iterator is still on a valid map element

说明

public bool WeakMap::valid ( void )

Returns whether the iterator is still on a valid map element.

参数

此函数没有参数。

返回值

Returns TRUE if the iterator is on a valid element that can be accessed, FALSE otherwise.