Skip to content

feat(extgen): add support for callable in parameters #1731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

alexandre-daubois
Copy link
Contributor

@alexandre-daubois alexandre-daubois commented Jul 7, 2025

Depends on #1724. 50e2a65 should be reviewed independently.

Allows to write such code:

// export_php:function my_array_map(array $data, callable $callback): array
func my_array_map(arr *C.zval, callback *C.zval) unsafe.Pointer {
	goArr := frankenphp.GoArray(unsafe.Pointer(arr))
	result := &frankenphp.Array{}

	for i := uint32(0); i < goArr.Len(); i++ {
		key, value := goArr.At(i)

                // Magic is here!
		callbackResult := frankenphp.CallPHPCallable(unsafe.Pointer(callback), []interface{}{value})

		if key.Type == frankenphp.PHPIntKey {
			result.SetInt(key.Int, callbackResult)
		} else {
			result.SetString(key.Str, callbackResult)
		}
	}

	return frankenphp.PHPArray(result)
}
$strArray = ['a' => 'hello', 'b' => 'world', 'c' => 'php'];
var_dump(my_array_map($strArray, 'strtoupper'));

$arr = [1, 2, 3, 4, [5, 6]];
var_dump(my_array_map($arr, function($item) {
    if (is_array($item)) {
        return my_array_map($item, function($subItem) {
            return $subItem * 2;
        });
    }

    return $item * 3;
}));

/*
Output:

array(3) {
  ["a"]=>
  string(5) "HELLO"
  ["b"]=>
  string(5) "WORLD"
  ["c"]=>
  string(3) "PHP"
}
array(5) {
  [0]=>
  int(3)
  [1]=>
  int(6)
  [2]=>
  int(9)
  [3]=>
  int(12)
  [4]=>
  array(2) {
    [0]=>
    int(10)
    [1]=>
    int(12)
  }
}
*/

TODO: fix support on methods

@withinboredom
Copy link
Member

IIRC, callables are not callable from arbitrary threads (I've tried this!). Meaning if you hold a callable reference and try to call it from another php thread, it will crash. This probably just needs to be documented for authors.

Objects are the same IIRC.

HOWEVER, opcache will copy all these pointers into a shared arena during compilation, so if you have opcache turned on, it might work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants