Book/weakref-Phpdoc专题
Weak References
目录
- 简介
- 安装/配置
- 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 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 <span
class="methodname">Weakref::__construct ( <span
class="methodparam">object $object )
public bool Weakref::acquire ( <span class="methodparam">void )
public object Weakref::get ( <span class="methodparam">void )
public bool Weakref::release ( <span class="methodparam">void )
public bool Weakref::valid ( <span class="methodparam">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 ( <span class="methodparam">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 <span
class="methodname">Weakref::__construct ( <span
class="methodparam">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 ( <span class="methodparam">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 ( <span class="methodparam">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 ( <span class="methodparam">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 <span class="oointerface">implements <span class="interfacename">Countable <span class="oointerface">, ArrayAccess , <span class="interfacename">Iterator {
/* 方法 */
public <span class="methodname">__construct ( <span class="methodparam">void )
public int <span class="methodname">count ( void )
public mixed current ( <span class="methodparam">void )
public object key ( <span class="methodparam">void )
public void next ( <span class="methodparam">void )
public bool
offsetExists ( <span
class="methodparam">object $object )
public mixed
offsetGet ( <span
class="methodparam">object $object )
public void
offsetSet ( <span
class="methodparam">object $object ,
mixed
$value )
public void
offsetUnset ( <span
class="methodparam">object $object )
public void rewind ( <span class="methodparam">void )
public bool valid ( <span class="methodparam">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 <span class="methodname">WeakMap::__construct ( <span class="methodparam">void )
Constructs a new map.
参数
此函数没有参数。
返回值
没有返回值。
WeakMap::count
Counts the number of live entries in the map
说明
public int <span class="methodname">WeakMap::count ( <span class="methodparam">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 ( <span class="methodparam">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 ( <span class="methodparam">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 ( <span class="methodparam">void )
Advances to the next map element.
参数
此函数没有参数。
返回值
没有返回值。
WeakMap::offsetExists
Checks whether a certain object is in the map
说明
public bool
WeakMap::offsetExists ( <span
class="methodparam">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 ( <span
class="methodparam">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 ( <span
class="methodparam">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 ( <span
class="methodparam">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 ( <span class="methodparam">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 ( <span class="methodparam">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.