Questions tagged [php-closures]

The tag has no usage guidance.

php-closures
Filter by
Sorted by
Tagged with
-1 votes
1 answer
2k views

Fatal error: Uncaught Error: Call to undefined function create_function() while activating wordpress plugin

I got an error while activating a plugin in my WordPress website Fatal error: Uncaught Error: Call to undefined function create_function() in /home4/seosexey/gifting.icowebtech.com/wp-content/plugins/...
Meena Arya's user avatar
-1 votes
2 answers
765 views

Laravel collection map use private function in same class instead closure

I use the map function to loop each object of collections. The closure method is too long and I tried to make it a private function. But I cannot successfully call the function. Here is my sample code....
Chao's user avatar
  • 179
1 vote
1 answer
113 views

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 ...
Erdss4's user avatar
  • 1,085
0 votes
2 answers
233 views

PHP closure return multiple functions

I'm trying to achieve almost similar things/properties of closure in PHP which is available in JS. For example function createGreeter($who) { return function(){ function hello() use ($...
Rupesh Arora's user avatar
0 votes
1 answer
35 views

PHP: access a variable from a class/method, both defined inside the same closure

The following code is working as expected: $b = 42; var_dump("b: " . $b); class A { function foo() { global $b; var_dump("b: " . $b); } } $instance = new A(); $instance-&...
sylbru's user avatar
  • 1,559
-1 votes
2 answers
78 views

Php Dynamic class methods

I have an array (class property) which stores PHP methods (i.e., of class 'Closure'). Just like this. $this->methods[$name]=$action; $action is the function. When I try invoking the function ...
Munavir Chavody's user avatar
1 vote
1 answer
44 views

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 ...
JackSlayer94's user avatar
0 votes
2 answers
46 views

Php require() does not recognize aliased class

So mainly this is caused by my code structure: File1.php use \Class1 as Blah; require 'File2.php'; File2.php $closure = function ($arg) { return new Blah($arg); }; Somehow the part behind ... ...
machinateur's user avatar
1 vote
2 answers
3k views

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 ...
Nik Gubin's user avatar
0 votes
1 answer
38 views

Pass Eloquent Model As An Closure Argument In PHP

I'm using Laravel Illuminate/Database outside of laravel application. I'm trying to pass the Eloquent model as my closure argument but its throwing an error. May be I'm passing it wrongly. My code is ...
Manish Jangir's user avatar
2 votes
2 answers
5k views

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 = ...
Ludwig's user avatar
  • 1,591
1 vote
1 answer
336 views

Unexpected behaviour in PHP Altorouter when passing parameters

Setup I'm accessing this url: <host>/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/[...
Marius Schär's user avatar
4 votes
1 answer
2k views

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

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 ...
Shoaib Nawaz's user avatar
  • 2,312
7 votes
2 answers
2k views

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 ...
Benubird's user avatar
  • 19.1k
1 vote
1 answer
616 views

How do you compare classes that contain `Closure`?

So, how do you compare classes that contain Closure ? It looks like you can't. class a { protected $whatever; function __construct() { $this->whatever = function() {}; } } ...
user2398964's user avatar
4 votes
1 answer
378 views

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 ...
astax's user avatar
  • 1,769
1 vote
1 answer
296 views

Slim protect callables

In Slim Framework, there is a protect function that wraps callables within a function (i.e. another callable). The description in the manual says: What if you want to literally store a closure as the ...
rink.attendant.6's user avatar
2 votes
0 answers
192 views

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 ...
John Sonderson's user avatar
1 vote
1 answer
391 views

Refactoring closures/anonymous functions in PHP

Is it possible to refactor the anonymous function out of code like this: function foo($path, $callback) { $callback(); } $app = array('a', 'b', 'c'); foo('vehicle/:id', function() use ($app) { ...
rink.attendant.6's user avatar
1 vote
1 answer
1k views

Validate field based on another field using additional database

I have a simple form, which contains two fields, the first field is just a select and the second field contains a value, which needs to be checked with the help of the first field. I have found a ...
CSchulz's user avatar
  • 11k
2 votes
1 answer
193 views

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 ...
Ben Harold's user avatar
  • 6,382
15 votes
5 answers
14k views

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, ...
John Sonderson's user avatar
8 votes
1 answer
2k views

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 ...
glen-84's user avatar
  • 1,838
1 vote
1 answer
166 views

Refactoring comparision/operators blocks to DRY up and reduce C.R.A.P level

I set out to make a small project around a bounch of classes that return generators (php 5.5). The main motivation for the small project was to expand on my TDD journey, fiddle with generators and ...
Ronni Skansing's user avatar
2 votes
1 answer
4k views

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 ...
Expedito's user avatar
  • 7,777
3 votes
1 answer
320 views

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) (...
Andrei Serdeliuc ॐ's user avatar
1 vote
2 answers
550 views

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 ...
cha0s's user avatar
  • 53