Динамически вкладывать выполнение функций с помощью array_reduce() и массива обратных вызовов.Php

Кемеровские программисты php общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Динамически вкладывать выполнение функций с помощью array_reduce() и массива обратных вызовов.

Сообщение Anonymous »


I am new to PHP and am currently messing around with array_reduce, when I see this happen:

php > echo array_reduce(array("(function($x){return $x;})"), function($p, $q) {return ($q)($p);}, "init"); Warning: Uncaught Error: Call to undefined function (function(, ){return ;})() in php shell code:1 Stack trace: #0 [internal function]: {closure}('init', '(function(, ){r...') #1 php shell code(1): array_reduce(Array, Object(Closure), 'init') #2 {main} thrown in php shell code on line 1 php > echo (function($x){return $x;})("init"); init Essentially, there are three parts to array_reduce() - the array, the reduce function and an initial value. Here my reduce function takes two strings func($x, $y) and calls $y with the argument $x - return ($y)($x);. So I expect that calling array_reduce(array(a1, a2, a3), ..., "init"), it will return a1(a2(a3("init"))) as repeated function calls.

Hence, I also pass in an anonymous function (function($x){return $x;}) as a1, hoping it will call correctly and return "init" as the final result. Indeed when running it on its own (see the second php command), it returns "init" correctly. However when passing into array_reduce(), it does not work. Can someone help explain why, preferably in simple terms? And is it possible to achieve what I want?


Источник: https://stackoverflow.com/questions/699 ... n-array-of
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Php»