KPHP functions extending PHP

Here we list functions, that extend PHP standard library and are available in KPHP natively.

Skip this page at first reading. Look through it later, when you start using KPHP.

Type system

These are keywords (language constructs), described in the type system article.

Functions for casting instances, described here.

arrays

Since KPHP doesn't support internal array pointers and functions like end(), it's a typed alternative.

Since PHP arrays can be keyed with both integers and strings, array_keys() returns mixed[].

But if you know, that your particular array carries only one type of keys, use these functions with clean types.

Swaps two elements in the array, like tmp = a[idx1]; a[idx1] = a[idx2]; a[idx2] = tmp, but more effeciently, especially for vectors.

Same as $dest = array_merge($dest, $another), but without creating a temporary array.

Find an element in an array with callback.
tuple[0] is corresponding key (null if not exists).
tuple[1] is a found value (empty T if not exists).

Since array_filter() in KPHP doesn't accept ARRAY_FILTER_USE_KEY, it's an alternative.
(as support of ARRAY_FILTER_USE_KEY can't be done without callback argument type spoiling)

Reserve array memory — if you know in advance, how many elements will be inserted to it.
Effectife especially for vectors, as there will be no reallocations on insertion.
$arr — target array (typically empty)
$int_keys_num — number of int keys
$str_keys_num — number of string keys
$is_vector — should it be a vector (if $str_keys_num is 0)

The same as array_reserve(), but takes all sizes (length, key type, is vector) from array $base.

Async programming

Read about coroutines (forks).

RPC calls

Read about TL schema and RPC calls.

Shared memory (instance cache)

Read about shared memory.

Memory stats

Returns an array ["$var_name" => size_in_bytes]
Don't use it in production — only to debug, which globals/statics allocate huge pieces of memory.
While compiling, a special env variable should be set: KPHP_ENABLE_GLOBAL_VARS_MEMORY_STATS=1

Returns currently used and dirty memory (in bytes).

Returns heap memory usage (system heap, not script allocator) (in bytes).

Serialization to binary format

Read about serialization and msgpack.

Profiling

Read about embedded profiler.

Misc

Same as PHP trigger_error($str, E_USER_WARNING); (KPHP doesn't have a trigger_error() function).

Useful for dev purposes: this callback is invoked when a runtime warning occurs.
It can be shown on screen for the developer, for example.
Do not use it in production! Use json log analyzer and trace C++ → PHP mapper instead.
$callback is invoked with 2 arguments: 1) $message: warning text; 2) $stacktrace: function names demangled from cpp trace, even without debug symbols — but slow, only for dev

Like register_kphp_on_warning_callback(), but it is not linked to any runtime error: instead, it allows getting current demangled backtrace at the execution point.
Note! Demangling works slowly, don't use it in high-loaded places!

Defines a context for runtime warnings (to be written to json error logs).
$tags — key-value tags (treated like an aggregate)
$extra_info — key-value extra arbitrary data (not an aggregator)
$env — environment (e.g.: staging / production)

Special intrinsic to wrap conditions inside if, like if (likely($x > 10)) — when you suppose that condition is much likely to happen than not (unlikely() is vice versa).
This can slightly pre-warm the CPU branch predictor.

All these functions work in plain PHP also — they are polyfilled to behave exactly like KPHP’s built-in.