Skip to content
Closed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ echo "Done\n";
7
Destroyed

Fatal error: Using $this when not in object context in %sclosure_005.php on line 28
Fatal error: Using $this when not in object context in %s on line 28
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions Zend/tests/closure_012.phpt → Zend/tests/closure/012.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ $lambda();
var_dump($i);
?>
--EXPECTF--
Notice: Undefined variable: i in %sclosure_012.php on line 2
Notice: Undefined variable: i in %s on line 2

Notice: Undefined variable: i in %sclosure_012.php on line 7
Notice: Undefined variable: i in %s on line 7
NULL
int(2)

File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions Zend/tests/closure_015.phpt → Zend/tests/closure/015.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ print $x;
print "\n";
?>
--EXPECTF--
Error: Object of class Closure could not be converted to string at %sclosure_015.php(8)
Error: Object of class Closure could not be converted to string at %s(8)

Error: Object of class Closure could not be converted to string at %sclosure_015.php(10)
Error: Object of class Closure could not be converted to string at %s(10)
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions Zend/tests/closure_018.phpt → Zend/tests/closure/018.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ var_dump($y, $x);

?>
--EXPECTF--
Notice: Only variable references should be returned by reference in %sclosure_018.php on line 7
Notice: Only variable references should be returned by reference in %s on line 7
int(4)

Notice: Only variable references should be returned by reference in %sclosure_018.php on line 7
Notice: Only variable references should be returned by reference in %s on line 7
int(16)
int(16)
int(16)
6 changes: 3 additions & 3 deletions Zend/tests/closure_019.phpt → Zend/tests/closure/019.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ test();

?>
--EXPECTF--
Notice: Only variable references should be returned by reference in %sclosure_019.php on line 4
Notice: Only variable references should be returned by reference in %s on line 4
int(9)

Notice: Only variable references should be returned by reference in %sclosure_019.php on line 4
Notice: Only variable references should be returned by reference in %s on line 4
int(81)

Fatal error: Cannot pass parameter 1 by reference in %s on line %d
Fatal error: Cannot pass parameter 1 by reference in %s on line %d
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ $foo = function() use ($a) {
$foo->a = 1;
?>
--EXPECTF--
Catchable fatal error: Closure object cannot have properties in %sclosure_022.php on line 5
Catchable fatal error: Closure object cannot have properties in %s22.php on line 5

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ $o->func();
--EXPECTF--
Test::{closure}()

Fatal error: Call to private method Test::func() from context '' in %sclosure_033.php on line %d
Fatal error: Call to private method Test::func() from context '' in %s on line %d
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions Zend/tests/closure/049.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Closure 049: static::class in static closure in non-static method.

--FILE--
<?php

class A {
function foo() {
$f = static function() {
return static::class;
};
return $f();
}
}

class B extends A {}

$b = new B;
var_dump($b->foo());
--EXPECT--
string(1) "B"
22 changes: 22 additions & 0 deletions Zend/tests/closure/050.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Closure 050: static::class in non-static closure in non-static method.

--FILE--
<?php

class A {
function foo() {
$f = function() {
return static::class;
};
return $f();
}
}

class B extends A {}

$b = new B;
var_dump($b->foo());

--EXPECT--
string(1) "B"
21 changes: 21 additions & 0 deletions Zend/tests/closure/051.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Closure 051: static::class in static closure in static method.

--FILE--
<?php

class A {
static function foo() {
$f = static function() {
return static::class;
};
return $f();
}
}

class B extends A {}

var_dump(B::foo());

--EXPECT--
string(1) "B"
21 changes: 21 additions & 0 deletions Zend/tests/closure/052.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Closure 052: static::class in non-static closure in static method.

--FILE--
<?php

class A {
static function foo() {
$f = function() {
return static::class;
};
return $f();
}
}

class B extends A {}

var_dump(B::foo());

--EXPECT--
string(1) "B"
22 changes: 22 additions & 0 deletions Zend/tests/closure/053.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Closure 053: self::class in static closure in non-static method.

--FILE--
<?php

class A {
function foo() {
$f = static function() {
return self::class;
};
return $f();
}
}

class B extends A {}

$b = new B;
var_dump($b->foo());

--EXPECT--
string(1) "A"
22 changes: 22 additions & 0 deletions Zend/tests/closure/054.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Closure 054: self::class in non-static closure in non-static method.

--FILE--
<?php

class A {
function foo() {
$f = function() {
return self::class;
};
return $f();
}
}

class B extends A {}

$b = new B;
var_dump($b->foo());

--EXPECT--
string(1) "A"
21 changes: 21 additions & 0 deletions Zend/tests/closure/055.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Closure 055: self::class in static closure in static method.

--FILE--
<?php

class A {
static function foo() {
$f = static function() {
return self::class;
};
return $f();
}
}

class B extends A {}

var_dump(B::foo());

--EXPECT--
string(1) "A"
21 changes: 21 additions & 0 deletions Zend/tests/closure/056.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Closure 056: self::class in non-static closure in static method.

--FILE--
<?php

class A {
static function foo() {
$f = function() {
return self::class;
};
return $f();
}
}

class B extends A {}

var_dump(B::foo());

--EXPECT--
string(1) "A"
36 changes: 36 additions & 0 deletions Zend/tests/closure/bug66622.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
Bug 66622: Closures do not correctly capture the late bound class (static::) in some cases

--FILE--
<?php
class A {
static function name() { return 'A'; }
function foo() {
$fn = function() { return static::name(); };
echo static::name() . ' vs ' . $fn() . "\n";
}
function bar() {
$fn = static function() { return static::name(); };
echo static::name() . ' vs ' . $fn() . "\n";
}
static function baz() {
$fn = function() { return static::name(); };
echo static::name() . ' vs ' . $fn() . "\n";
}
}
class B extends A {
static function name() { return 'B'; }
}
function test() {
(new B)->foo();
(new B)->bar();
(new B)->baz();
B::baz();
}
test();

--EXPECT--
B vs B
B vs B
B vs B
B vs B
4 changes: 1 addition & 3 deletions Zend/zend_closures.c
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_ent
}
}

closure->this_ptr = NULL;
/* Invariants:
* If the closure is unscoped, it has no bound object.
* The the closure is scoped, it's either static or it's bound */
Expand All @@ -491,10 +492,7 @@ ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_ent
Z_ADDREF_P(this_ptr);
} else {
closure->func.common.fn_flags |= ZEND_ACC_STATIC;
closure->this_ptr = NULL;
}
} else {
closure->this_ptr = NULL;
}
}
/* }}} */
Expand Down
8 changes: 7 additions & 1 deletion Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -5408,7 +5408,13 @@ ZEND_VM_HANDLER(153, ZEND_DECLARE_LAMBDA_FUNCTION, CONST, UNUSED)
zend_error_noreturn(E_ERROR, "Base lambda function for closure not found");
}

zend_create_closure(&EX_T(opline->result.var).tmp_var, (zend_function *) op_array, EG(scope), EG(This) TSRMLS_CC);
int defined_function_is_static = op_array->common.fn_flags & ZEND_ACC_STATIC;
int function_is_being_defined_inside_static_context = EX(prev_execute_data) && EX(prev_execute_data)->function_state.function->common.fn_flags & ZEND_ACC_STATIC;
if (defined_function_is_static || function_is_being_defined_inside_static_context) {
zend_create_closure(&EX_T(opline->result.var).tmp_var, (zend_function *) op_array, EG(called_scope), NULL TSRMLS_CC);
} else {
zend_create_closure(&EX_T(opline->result.var).tmp_var, (zend_function *) op_array, EG(scope), EG(This) TSRMLS_CC);
}

CHECK_EXCEPTION();
ZEND_VM_NEXT_OPCODE();
Expand Down
8 changes: 7 additions & 1 deletion Zend/zend_vm_execute.h
Original file line number Diff line number Diff line change
Expand Up @@ -6789,7 +6789,13 @@ static int ZEND_FASTCALL ZEND_DECLARE_LAMBDA_FUNCTION_SPEC_CONST_UNUSED_HANDLER
zend_error_noreturn(E_ERROR, "Base lambda function for closure not found");
}

zend_create_closure(&EX_T(opline->result.var).tmp_var, (zend_function *) op_array, EG(scope), EG(This) TSRMLS_CC);
int defined_function_is_static = op_array->common.fn_flags & ZEND_ACC_STATIC;
int function_is_being_defined_inside_static_context = EX(prev_execute_data) && EX(prev_execute_data)->function_state.function->common.fn_flags & ZEND_ACC_STATIC;
if (defined_function_is_static || function_is_being_defined_inside_static_context) {
zend_create_closure(&EX_T(opline->result.var).tmp_var, (zend_function *) op_array, EG(called_scope), NULL TSRMLS_CC);
} else {
zend_create_closure(&EX_T(opline->result.var).tmp_var, (zend_function *) op_array, EG(scope), EG(This) TSRMLS_CC);
}

CHECK_EXCEPTION();
ZEND_VM_NEXT_OPCODE();
Expand Down