Skip to content

Conversation

@alexandre-daubois
Copy link
Member

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

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 {
	goSlice, err := frankenphp.GoPackedArray[any](unsafe.Pointer(arr))
	if err != nil {
		panic(err)
	}

	result := make([]any, len(goSlice))

	for index, value := range goSlice {
	  result[index] = frankenphp.CallPHPCallable(unsafe.Pointer(callback), []interface{}{value})
	}

	return frankenphp.PHPPackedArray(result)
}
use function Go\Extension\my_array_map;

$a = [1, 2, 3, 4, 5];

$b = my_array_map($a, function ($item) {
    return $item * 2;
});

var_dump($b);

/*
Output:

array(5) { [0]=> int(2) [1]=> int(4) [2]=> int(6) [3]=> int(8) [4]=> int(10) }
*/

@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.

@alexandre-daubois
Copy link
Member Author

alexandre-daubois commented Jul 25, 2025

Meaning if you hold a callable reference and try to call it from another php thread, it will crash.

This is very unlikely I guess, but it doesn't hurt to write it in the doc indeed! Let's say that the behavior is undefined and could be unexpected

@alexandre-daubois alexandre-daubois marked this pull request as ready for review July 25, 2025 09:11
@alexandre-daubois alexandre-daubois force-pushed the callable-support branch 2 times, most recently from 71440c2 to 06231ef Compare November 14, 2025 09:44
@alexandre-daubois alexandre-daubois marked this pull request as draft November 14, 2025 11:18
@alexandre-daubois alexandre-daubois force-pushed the callable-support branch 2 times, most recently from d832efe to c212df2 Compare November 21, 2025 13:27
@alexandre-daubois alexandre-daubois marked this pull request as ready for review November 21, 2025 13:27
@alexandre-daubois
Copy link
Member Author

alexandre-daubois commented Nov 21, 2025

@php/frankenphp-collaborators this one's ready for review as well 🙂 It would be better to wait for #1894 to be merged before this one however

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