Types and values

Working with zvals

phpz.Zval is a zero-overhead wrapper around *c.zval. It does not allocate storage, increment references or release values automatically. ctx.retval points to PHP’s return slot. ctx.ret(kind, value) calls ctx.retval.set(kind, value).

Local temporary values

var temporary = phpz.Zval.raw.undef;
defer phpz.Zval.raw.release(&temporary);

const value = phpz.Zval.from(&temporary);
value.set(.string, "phpz");
ctx.ret(.mixed, value.ptr());

set(.string, ...) creates a string. ctx.ret(.mixed, ...) copies the dereferenced value and retains it as needed, so the temporary still requires release. release() accepts Zend undef. Zig undefined storage must be initialized first.

A native Zend bailout can skip this Zig defer. See Bailouts and cleanup for cleanup boundaries.

Set values

set(kind, value) writes a type and value without destroying the old contents. Before overwriting an owned value, release it.

kindZig argumentBehavior
.undef{}Write Zend’s internal undefined state. Avoid exposing it as a public return value
.null{}Write PHP null
.inti64Store the integer directly
.floatf64Store the floating-point value directly
.boolboolEncode true or false in the type tag
.string[]const u8Copy the bytes by length. The zval owns the string
.str*phpz.zend.StringTransfer one string reference without copying bytes. Call copy() on a borrowed input first
.array*phpz.zend.ArrayStore the pointer without addref
.object*phpz.zend.ObjectStore the pointer without addref
.resource*phpz.zend.ResourceStore the pointer without addref
.reference*phpz.zend.ReferenceStore the pointer without addref
.mixed*c.zvalCopy the dereferenced value and increment its reference count as needed

.indirect and .ptr are internal Zend states and cannot be written with set. For a PHP return value use ctx.ret(kind, value).

Reading values and checking types

OperationBehavior
kind()Return the Zval.Kind
is(kind)Test for the specified type
as(kind)Check the type and read it, or return error.TypeMismatch
asOrDefault(kind, default)Return the default if as fails
asUnchecked(kind)Read without checking. Use only with a confirmed type
ptr()Borrow the underlying *c.zval
if (value.is(.int)) {
    const n = value.asUnchecked(.int);
    _ = n;
}

const count = value.asOrDefault(.int, 0);

Use cast(kind) for a PHP explicit cast or convert(kind) to replace the current value in place. See Type conversions for effects on the source and reference counts.

Ownership and release

Read operations usually borrow arrays, objects, resources and reference wrappers. Call addref or copy as appropriate to extend a value’s lifetime. Use the following operations to manage ownership:

OperationBehavior
release()Release one owned value and destroy it at count zero. Accepts undef and leaves the slot uncleared
makeRef()Wrap the value in a PHP reference in place. An existing reference stays unchanged. A new container starts at count 1
addref() / tryAddref()Retain a reference and preserve the PHP reference wrapper. The former requires a refcounted value and the latter checks first
delref() / tryDelref()Only decrement, even at zero. The former requires a refcounted value and the latter checks first
refcount()Read the payload count. Requires a refcounted value or an array, asserted in Debug builds
Zval.raw.copy(dst, src)Copy the zval, retaining as needed and preserving the reference wrapper
Zval.raw.copyDeref(dst, src)Dereference the source, then copy and retain as needed
Zval.raw.copyValue(dst, src)Copy just the type and value without incrementing a reference

Zval.raw provides the same reference count and release operations. Zval.raw.makeRef(zv) corresponds to ZVAL_MAKE_REF. It wraps the current value without copying its contents or nesting an existing reference. For PHP by-reference returns, use it on an existing value or call zend.Reference.init directly. Avoid refcount() on a non-countable value or at count zero.

Use release() for cleanup. To reuse a released temporary zval, set it to Zval.raw.undef first.

undef is Zend’s internal undefined state. To return PHP null, use set(.null, {}).