//create a new object $myObj = new stdClass(); //get the object's id $id = spl_object_hash($myObj); //display the object's id echo "The object's id is: " . $id;
//create a new object class MyClass { //class content } $obj = new MyClass(); //get the object's id $objId = spl_object_hash($obj); //display the object's id echo "My object's id is: " . $objId;These examples use the spl_object_hash() function to return the object's id or hash value. This function is available as part of the Standard PHP Library (SPL). In conclusion, the get_id() method belongs to PHP's core functionality and does not require any external package or library. It is a quick and easy way to get the unique identifier of an object, which can be useful in certain situations, such as debugging code or tracking objects.