Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 5.4 - 'closure $this support'

Tags:

closures

php

I see that the new planned features for PHP 5.4 are: traits, array dereferencing, a JsonSerializable interface and something referred to as 'closure $this support'

http://en.wikipedia.org/wiki/Php#Release_history

While the others are either immediately clear (JsonSerialiable, array dereferencing) or i looked up the specifics (traits), I am not sure what 'closure $this support' is. I have been unsuccessful googling for it or finding anything about it on php.net

Does anyone know what this is supposed to be?

If i had to guess, it would mean something like this:

$a = 10; $b = 'strrrring'; //'old' way, PHP 5.3.x $myClosure = function($x) use($a,$b)              {                  if (strlen($x) <= $a) return $x;                  else return $b;              };  //'new' way with closure $this for PHP 5.4 $myNewClosure = function($x) use($a as $lengthCap,$b as $alternative)                  {                      if(strlen($x) <=  $this->lengthCap)) return $x;                      else                       {                          $this->lengthCap++;  //lengthcap is incremented for next time around                          return $this->alternative;                      }                  }; 

The significance (even if this example is trivial) being that in the past once the closure is constructed the bound 'use' variables are fixed. With 'closure $this support' they are more like members you can mess with.

Does this sound correct and/or close and/or reasonable? Does anyone know what this 'closure $this support' means?

like image 339
jon_darkstar Avatar asked Apr 20 '11 17:04

jon_darkstar


People also ask

Does PHP have closure?

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 PHP closure class?

The Closure class ¶Class used to represent anonymous functions. Anonymous functions yield objects of this type. This class has methods that allow further control of the anonymous function after it has been created. Besides the methods listed here, this class also has an __invoke method.

What is object closure PHP?

A closure is an object that can be called like a function. As you can see from the output, when you use $lat in print_r($lat); , it is not the result of calling the closure, it is the closure object itself. (Defined by $lat = function () {... - see example 2 in the PHP documentation for anonymous functions), .

What is a closure in PHP and why does it use the 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. use is early binding. That means the variable values are COPIED upon DEFINING the closure.


1 Answers

This was already planned for PHP 5.3, but

For PHP 5.3 $this support for Closures was removed because no consensus could be reached how to implement it in a sane fashion. This RFC describes the possible roads that can be taken to implement it in the next PHP version.

It indeed means you can refer to the object instance (live demo)

<?php class A {   private $value = 1;   public function getClosure()    {     return function() { return $this->value; };   } }  $a = new A; $fn = $a->getClosure(); echo $fn(); // 1 

For a discussion, see the PHP Wiki

  • Closures: Object extension

and for historic interest:

  • closures (rfc)
  • removal-of-this (rfc:closures)
like image 151
Gordon Avatar answered Sep 24 '22 21:09

Gordon