Skip to content

Core\Program::id

Returns a text that identifies this version of your program.

Core\Program::id() returns 64 lowercase hexadecimal characters. The value is a hash of all the code in your program and of the environment it was compiled for. The same code on the same server gives the same value on every run. When you deploy a change, the value changes too.

This makes it a good key for anything that must be thrown away after a deployment: a cache entry, a version label in a log, or a header that tells a browser to load a file again.

Good to know: the value is safe to print. A hash does not show any of your source code.

The examples below print the length of the id, make a short version label from it, and build cache keys that change with every deployment.

Return value

Returns string. 64 lowercase hexadecimal characters. The value changes when the code or the environment changes. It is safe to print, because a hash does not show any source code.

Errors

  • LogicErrorThe program was started without an identity. nvs run always gives a program one, so this happens only when a program is started some other way.

Examples

What the id looks like

<?nvs
// Shows what `Core\Program::id` returns. The value itself depends on your code,
// so this example prints only facts that are the same everywhere.
string $id = Core\Program::id();
// The id has 64 characters. This prints `64`.
echo Core\Str::length($id), "\n";
// Two calls in one run return the same value. This prints `same`.
echo $id == Core\Program::id() ? 'same' : 'different', "\n";
Output
64
same

A short version label

<?nvs
// Shows how to make a short version label, for example for a log line or the
// footer of an admin page.
// `Core\Str::slice` takes the first 12 characters of the id.
string $version = Core\Str::slice(Core\Program::id(), 0, 12);
// This prints `12`.
echo Core\Str::length($version), "\n";
// The label is the start of the full id. This prints `true`.
echo Core\Str::startsWith(Core\Program::id(), $version) ? 'true' : 'false', "\n";
Output
12
true

Cache keys that change with each deployment

<?nvs
// Shows how to build cache keys that change when you deploy new code. After a
// deployment, the old entries are never read again.
class PageCache
{
public static function key(string $page): string
{
return 'page:' . Core\Str::slice(Core\Program::id(), 0, 16) . ':' . $page;
}
}
string $home = PageCache::key('home');
// The key has the prefix, 16 characters of the id, a colon and the page name.
// This prints `26`.
echo Core\Str::length($home), "\n";
// This prints `true`.
echo Core\Str::endsWith($home, ':home') ? 'true' : 'false', "\n";
// The same page gives the same key during one run. This prints `true`.
echo PageCache::key('home') == $home ? 'true' : 'false', "\n";
Output
26
true
true