xref: /PHP-8.3/ext/ffi/tests/addr_in_lval.phpt (revision adaf7263)
1--TEST--
2Assignment to CDATA though FFI::addr() trick
3--EXTENSIONS--
4ffi
5--INI--
6ffi.enable=1
7--FILE--
8<?php
9$f = FFI::cdef("typedef struct { char *bar; } other;");
10class Container {
11    public $data;
12    function __construct($f) { $this->data = $f->new("other"); }
13    function &getBar() { return $this->data->bar; } // return by ref to get CData instead of null
14}
15$container = new Container($f);
16$data = $f->new("char[2]");
17$data[0] = "1";
18FFI::addr($container->getBar())[0] = $f->cast("char*", $data); // directly write it
19var_dump($container);
20?>
21--EXPECT--
22object(Container)#2 (1) {
23  ["data"]=>
24  object(FFI\CData:struct <anonymous>)#3 (1) {
25    ["bar"]=>
26    object(FFI\CData:char*)#6 (1) {
27      [0]=>
28      string(1) "1"
29    }
30  }
31}
32