Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use $this in closure in php

Tags:

I have function like this:

class Service {     function delete_user($username) {            ...         $sessions = $this->config->sessions;         $this->config->sessions = array_filter($sessions, function($session) use ($this){             return $this->get_username($session->token) != $username;         });     } } 

but this don't work because you can't use $this inside use, is it possible to execute function which is member of class Service inside a callback? Or do I need to use for or foreach loop?

like image 616
jcubic Avatar asked Jun 11 '17 09:06

jcubic


People also ask

How are closures established in PHP?

Basically a closure in PHP is a function that can be created without a specified name - an anonymous function. Here's a closure function created as the second parameter of array_walk() . By specifying the $v parameter as a reference one can modify each value in the original array through the closure function.

What is closure in PHP and why does it use use identifier?

A closure is a separate namespace, normally, you can not access variables defined outside of this namespace. There comes the use keyword: use allows you to access (use) the succeeding variables inside the closure.

How can you pass a local variable to an anonymous function in PHP?

Yes, use a closure: functionName($someArgument, function() use(&$variable) { $variable = "something"; }); Note that in order for you to be able to modify $variable and retrieve the modified value outside of the scope of the anonymous function, it must be referenced in the closure using & . It's new!

What is a closure function PHP?

A closure is an anonymous function that can access variables imported from the outside scope without using any global variables. Theoretically, a closure is a function with some arguments closed (e.g. fixed) by the environment when it is defined. Closures can work around variable scope restrictions in a clean way.

What are closures in PHP and how to use them?

Closures have been introduced in PHP 5.3 and their most important use is for callback functions. Basically a closure in PHP is a function that can be created without a specified name - an anonymous function. Here's a closure function created as the second parameter of array_walk ().

Can multiple functions share the same closure in PHP?

It's possible for multiple functions to share the same closure, and they can have access to multiple closures as long as they are within their accessible scope. In PHP, a closure is a callable class, to which you've bound your parameters manually.

What is closure in Python?

Well, closure is nothing but an object representation of anonymous function. It is the object-oriented way to use anonymous function. More interestingly, the above anonymous function example we just saw, actually returns a reference of Closure object, not only a function reference.

What is a closure in JavaScript?

In JavaScript, a closure can be thought of as a scope, when you define a function, it silently inherits the scope it's defined in, which is called its closure, and it retains that no matter where it's used.


1 Answers

$this is always available in (non-static) closures since PHP 5.4, no need to use it.

class Service {     function delete_user($username) {            ...         $sessions = $this->config->sessions;         $this->config->sessions = array_filter($sessions, function($session) {             return $this->get_username($session->token) != $username;         });     } } 

See PHP manual - Anonymous functions - Automatic binding of $this

like image 179
ShiraNai7 Avatar answered Oct 09 '22 15:10

ShiraNai7