I realized, that closures are consuming more memory. Could someone help me to understand why it is happening like that, please? Also, having following results, is it not recommended to use closures?
$value = 10;
$memory = memory_get_usage();
$fun = static function ($add, $value) {
$value += $add;
echo $value;
};
$fun(10, $value);
echo ' | ' . $value . ' | memory: ' . (memory_get_usage() - $memory);
Output:
20 | 10 | memory: 368
$value = 10;
$memory = memory_get_usage();
$closure = static function (int $add) use ($value) {
$value += $add;
echo $value;
};
$closure(10);
echo ' | ' . $value . ' | memory: ' . (memory_get_usage() - $memory);
Output:
20 | 10 | memory: 752
(the same is happening for objects)