Php/docs/class.weakref

提供:Dev Guides
< Php
移動先:案内検索

(PECL weakref >= 0.1.0)

はじめに

WeakRef クラスはオブジェクトへのゲートウェイを提供します。 ガベージコレクタがオブジェクトを解放するのを邪魔しません。 また、弱い参照を強い参照に切り替える方法も用意します。

注意:

WeakRef クラスは、 WeakReference クラスと混同させる意図はありません。

クラス概要


WeakRef {

/* メソッド */

public Weakref::__construct ( object $object )

public Weakref::acquire ( ) : bool

public Weakref::get ( ) : object

public Weakref::release ( ) : bool

public Weakref::valid ( ) : bool

}

例1 WeakRef の使用例

<?phpclass 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!

目次