Get Instance ID of an Object in PHP

前端 未结 10 1682
说谎
说谎 2020-12-14 07:19

I\'ve learn a while ago on StackOverflow that we can get the \"instance ID\" of any resource, for instance:

var_dump(intval(curl_init()));  // int(2)
var_dum         


        
10条回答
  •  情话喂你
    2020-12-14 07:46

    Alix, your solution in the question was exactly what I needed, but actually breaks when there's an object in an object, returns the last # in the var_dump. I fixed this, made the regex faster, and put it in a nice little function.

    /**
     * Get global object ID
     * From: http://stackoverflow.com/questions/2872366/get-instance-id-of-an-object-in-php
     * By: Alix Axel, non-greedy fix by Nate Ferrero
     */
    function get_object_id(&$obj) {
        if(!is_object($obj))
            return false;
        ob_start();
        var_dump($obj);// object(foo)#INSTANCE_ID (0) { }
        preg_match('~^.+?#(\d+)~s', ob_get_clean(), $oid);
        return $oid[1]; 
    }
    

提交回复
热议问题