php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53573 Invisible "static" property of Closure
Submitted: 2010-12-19 06:03 UTC Modified: 2012-09-11 21:08 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: kak dot serpom dot po dot yaitsam at gmail dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 5.3.4 OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2010-12-19 06:03 UTC] kak dot serpom dot po dot yaitsam at gmail dot com
Description:
------------
var_dump($closure) displays public property named "static", but I cannot access to 
it with standard call: Closure object cannot have properties
It might be very useful!
Thanks.

Test script:
---------------
<?php
$a = function() {
	static $foo = 'bar';
	echo $foo."\n";
};
var_dump($a);
$a->{'static'}['foo'] = 'foo';
$a();


Expected result:
----------------
Output: foo

Actual result:
--------------
object(Closure)#1 (1) {
  ["static"]=>
  array(1) {
    ["foo"]=>
    string(3) "bar"
  }
}
PHP Catchable fatal error:  Closure object cannot have properties in 
/home/web/1.php on line 7


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-16 01:51 UTC] olamedia at gmail dot com
<?php
class a{
  public $c = array();
  function __call($name, $args){
    $closure = $this->c[$name]; // example
    $self = $this;
    // how can I pass $self to closure?
    $closure->static['self'] = $this // NO!, Reflection is giving empty array
    call_user_func_array($closure, $args);
  }
}
$a = new a();
$a->c['hello'] = function($x) use ($self){
  return 'Hello, '.$x.'! My name is '.get_class($self).'!';
};
$a->hello('php');
 [2011-02-16 02:04 UTC] olamedia at gmail dot com
Please, let Closure be standard object without any restrictions.
Let developers decide what they can use, and what must not.
PHP is already full enough of "RESERVED" "YOU MUST NOT" "YOU CAN NOT" "YOU SHOULD 
NOT" "ITS HARD, SO WE WILL NOT MAKE THIS FEATURE" "I DONT NEED THIS, SO PLEASE 
DON'T REQUEST THIS FEATURE"
 [2011-11-15 23:32 UTC] felipe@php.net
-Type: Bug +Type: Feature/Change Request
 [2012-09-11 18:16 UTC] reeze@php.net
In fact, closure is a class: Closure just an implementation detail.

we should forget about the class itself. the output static is just 
let you to ease debugging but not for public access
 [2012-09-11 21:08 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2012-09-11 21:08 UTC] nikic@php.net
As reeze pointed out this is only debugging output (var_dump is a debugging function). The property does not actually exist. We provide the same kind of functionality for various other classes. You could think of this as a "private" property, which is used internally, but which the user does not have access too (encapsulation).

I see no reason to expose this. If you need this for $this bindings, then switch to PHP 5.4. It will automatically bind $this and allows rebinding using the ->bind() method.

Closing this as won't fix. (If you have any convincing counter arguments, please share them and this will be reopened.)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 01:01:30 2024 UTC