Questions tagged [php-closures]
27 questions
1
vote
1 answer
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() {};
}
}
$b = new a();
$c = new a();
var_dump( $b == $c ); …

user2398964
- 37
- 5
1
vote
1 answer
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 raw value and not have it invoked? You can do…

rink.attendant.6
- 44,500
- 61
- 101
- 156
1
vote
1 answer
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) {
echo $app[0];
});
I tried this but it did not…

rink.attendant.6
- 44,500
- 61
- 101
- 156
1
vote
1 answer
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 similar question Symfony2 form validation based on…

CSchulz
- 10,882
- 11
- 60
- 114
1
vote
1 answer
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 have a package I could throw on packagist for later…

Ronni Skansing
- 1,494
- 1
- 17
- 33
0
votes
2 answers
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 ($who) {
echo "Hi $who";
}
…

Rupesh Arora
- 557
- 2
- 9
- 26
0
votes
1 answer
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->foo();
The foo method is able to access $b…

sylbru
- 1,461
- 1
- 11
- 19
0
votes
2 answers
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 ... as isn't recognized after using require().

machinateur
- 502
- 5
- 10
0
votes
1 answer
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 as following:
// Create a dummy subject…

Manish Jangir
- 5,329
- 4
- 42
- 75
-1
votes
1 answer
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…

Meena Arya
- 9
- 2
-1
votes
2 answers
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. (note: the function test may be very long in…

Chao
- 179
- 1
- 8
-1
votes
2 answers
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 like $this->methods[$name](), I'm unable to access $this…

Munavir Chavody
- 489
- 4
- 16