php - Generate ID based on file and line in (or something similar) -
in program need way create unique identifiers (like hash). important ids not random, same between calls same function or method. thus, if in line in file generated identifier xyz
, in place should generated same identifier.
i use code such instruction:
$id = sha1(__file__ . __ line__);
but not enough comfortable. i'd prefer use function or macro in c/c++ (i know php not have macros) follows:
$id = generate_id();
the generate_id
implemented below. works fine, i'm not convinced use function debug_backtrace
in production code. maybe there's better solution?
function generate_id() { $backtrace = debug_backtrace(); return sha1($backtrace[0]['file'] . $backtrace[0]['line']); }
if intention have repeatable hash based on file, php has md5_file() method should need.
$file = 'myfile.txt'; echo 'md5 file hash of ' . $file . ': ' . md5_file($file);
as long file contents not change, md5 hash same each time generate it.
Comments
Post a Comment