diff --git a/composer.json b/composer.json index 549af01f4..c7305f122 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,8 @@ "Psalm\\": "src/Psalm" }, "files": [ - "src/functions.php" + "src/functions.php", + "src/spl_object_id.php" ] }, "autoload-dev": { diff --git a/src/spl_object_id.php b/src/spl_object_id.php new file mode 100644 index 000000000..cb1dfd0d1 --- /dev/null +++ b/src/spl_object_id.php @@ -0,0 +1,52 @@ +isUserDefined()) { + /** + * See https://github.com/runkit7/runkit_object_id for a faster native version for php <= 7.1 + * + * @param object $object + * @return int The object id + */ + function spl_object_id($object) : int + { + return runkit_object_id($object); + } + } elseif (PHP_INT_SIZE === 8) { + /** + * See https://github.com/runkit7/runkit_object_id for a faster native version for php <= 7.1 + * + * @param object $object + * @return int (The object id, XORed with a random number) + */ + function spl_object_id($object) : int + { + $hash = spl_object_hash($object); + // Fit this into a php long (32-bit or 64-bit signed int). + // The first 16 hex digits (64 bytes) vary, the last 16 don't. + // Values are usually padded with 0s at the front. + return intval(substr($hash, 1, 15), 16); + } + } else { + /** + * See https://github.com/runkit7/runkit_object_id for a faster native version for php <= 7.1 + * + * @param object $object + * @return int (The object id, XORed with a random number) + */ + function spl_object_id($object) : int + { + $hash = spl_object_hash($object); + // Fit this into a php long (32-bit or 64-bit signed int). + // The first 16 hex digits (64 bytes) vary, the last 16 don't. + // Values are usually padded with 0s at the front. + return intval(substr($hash, 9, 7), 16); + } + } +}