Get the reference count of an object in PHP?

后端 未结 5 2299
终归单人心
终归单人心 2021-01-01 18:26

I realize the knee-jerk response to this question is that \"you dont.\", but hear me out.

Basically I am running on an active-record system on a SQL, and in order to

5条回答
  •  滥情空心
    2021-01-01 19:27

    PHP 7.4 now has WeakReference

    To know if $obj is referenced by something else or not, you could use:

    // 1: create a weak reference to the object
    $wr = WeakReference::create($obj);
    
    // 2: unset our reference
    unset($obj);
    
    // 3: test if the weak reference is still valid
    $res = $wr->get();
    if (!is_null($res)) {
        // a handle to the object is still held somewhere else in addition to $obj
        $obj = $res;
        unset($res);
    }
    

提交回复
热议问题