Questions tagged [php-closures]

27 questions
15
votes
5 answers

Difference between a lambda function and a closure (in PHP)?

Chapter 2 of "Magento PHP Developer's Guide" states: Zend Framework 2 uses 100% object-oriented code and utilizes most of the new features of PHP 5.3, namely namespaces, late static binding, lambda functions and closures. While the post What…
John Sonderson
  • 3,238
  • 6
  • 31
  • 45
8
votes
1 answer

Factory classes vs closures in Zend Framework 2

Is it better to use factory classes or closures in Zend Framework 2, and why? I know that closures cannot be serialized, but if you return them from Module#getServiceConfig(), this will not affect the caching of the rest of your configuration data,…
glen-84
  • 1,778
  • 2
  • 18
  • 30
7
votes
2 answers

How to inspect a closure in php?

I have a function that is being passed a Closure. I want to find out the name of the method that closure is derived from. When I call print_r, it outputs this: Closure Object ( [static] => Array ( [listener] =>…
Benubird
  • 18,551
  • 27
  • 90
  • 141
4
votes
1 answer

Is there a way to update caller scope variables from php closure

use keyword with php closure is a pretty clear way to extend the scope of handpicked variable to closure. Is there any way if we need to update the value of some variable in caller function scope from closure? $total_strength =…
Shoaib Nawaz
  • 2,302
  • 4
  • 29
  • 38
4
votes
1 answer

Silex service - $app parameter or "use ($app)" statement?

If I define a service in Silex application, I can either expect the main container (Application) to be passed as a parameter or can take it from the current scope using "use ($app)" statement. The official documentation at…
astax
  • 1,769
  • 1
  • 14
  • 23
3
votes
1 answer

No closure support in PHP 5.3.2-1ubuntu4.2

I just realised that a bit of PHP doesn't execute correctly on one server, but it does on another. They're both running Ubuntu 10.04 with PHP PHP 5.3.2 (PHP 5.3.2-1ubuntu4.2 with Suhosin-Patch (cli) (built: May 13 2010 20:03:45)) I'm testing…
Andrei Serdeliuc ॐ
  • 5,828
  • 5
  • 39
  • 66
2
votes
2 answers

Laravel eloquent foreach loop

I am having a foreach loop that goes through an array, and saves data with eloquent. And it works fine when it is like this: foreach($questions['questions'] as $question) { $questionObject = Question::create([ 'external_id' =>…
Ludwig
  • 1,401
  • 13
  • 62
  • 125
2
votes
0 answers

PHP closure functions: why does a closure have to be an anonymous function?

A lambda or anonymous function is just a function without a name. e.g. $lambda = function($a, $b) { echo $a + $b; }; A closure is a function which has access to variables not specified in its parameter list. In PHP 5.3+ these variables are…
John Sonderson
  • 3,238
  • 6
  • 31
  • 45
2
votes
1 answer

Is there a good reason to use a closure instead of a for loop in this case?

I'm using Laravel 4; the models in question extend Eloquent. I've got a relationship between two models, listing and photo. I'm compiling a collection of listings that do not have any corresponding photos. Eloquent has a handy method for finding…
Ben Harold
  • 6,242
  • 6
  • 47
  • 71
2
votes
1 answer

Parallel cURL Request with WRITEFUNCTION Callback

I'm trying to limit my cURL responses as suggested in these posts:Retrieve partial web page and PHP CURLOPT_WRITEFUNCTION doesn't appear to be working. The idea is to limit the response to 4000 characters as specified in the callback function. I…
Expedito
  • 7,771
  • 5
  • 30
  • 43
1
vote
1 answer

PHP automatic binding of $this, is it a copy or an extend?

When using an anonymous PHP function that is defined inside a class context, the docs say "the current class is automatically bound to it, making $this available inside of the function's scope". But I'm a little confused what that means, does it…
Erdss4
  • 1,025
  • 3
  • 11
  • 31
1
vote
1 answer

Value of $total not being set inside lumen collection map function

I have a lumen collection object for my order and use map function to iterate over it and do some logic. I also need the total value of the order which is calculated using quantity * price. But the variable that is responsible for holding the total…
JackSlayer94
  • 805
  • 3
  • 16
  • 38
1
vote
2 answers

Laravel Commands, Pthreads and Closure

There is a need to perform a specific process multiple threads. I learned about the extension for php - pthreads. For example, a simple script outside Laravel works fine and I liked the results. I decided to move in Laravel, and faced with the…
Nik Gubin
  • 11
  • 1
  • 6
1
vote
1 answer

Unexpected behaviour in PHP Altorouter when passing parameters

Setup I'm accessing this url: /render/z63034/RBLR/GLZB. My url pattern is as such: /render/[a:title]/[a:bpFrom]/[a:bpTo]. My route gets added like so: $router->map("GET", "/render/[a:title]/[a:bpFrom]/[a:bpTo]", function ($params) { include…
Marius Schär
  • 336
  • 2
  • 17
1
vote
2 answers

Do PHP closures not have access to parnt function parameters?

I've been writing some code for PHP 5.3, and I wanted to do something similar to the code I'm showing below. I expect this code to print 'hellohello', but it prints 'hello' instead, and an error. It appears the $inner closure does not have access to…
cha0s
  • 53
  • 3
1
2