cData = $cData; FFI::memcpy($cData, $data, $len); $slice = $ffi->new('GoSlice'); if ($slice === null) throw new RuntimeException('error allocating buffer'); $this->slice = $slice; $slice->data = FFI::addr($cData); // $cData must not be destroyed while $slice is in use $slice->cap = $slice->len = $len; } public function slice(): CData { return $this->slice; } public function __destruct() { // Make sure we do not unknowingly use a slice with deallocated data $this->slice->data = null; $this->slice->cap = $this->slice->len = 0; } }