Skip to content

Commit 6ef8ae6

Browse files
committed
Allow self etc in eval / file scope
This fixes a regression from 16a9bc1. Together with the recent closure related changes this should allow all usages of self etc, while previously (in PHP 5) some things like self::class did not work.
1 parent ab4ccff commit 6ef8ae6

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
--TEST--
2-
Accessing self::FOO outside a class
2+
Accessing self::FOO in a free function
33
--FILE--
44
<?php
5-
var_dump(self::FOO);
5+
function test() {
6+
var_dump(self::FOO);
7+
}
68
?>
79
--EXPECTF--
810
Fatal error: Cannot use "self" when no class scope is active in %s on line %d

Zend/tests/self_in_eval.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
self etc. can be used in eval() in a class scope
3+
--FILE--
4+
<?php
5+
6+
class C {
7+
const FOO = 1;
8+
private static $bar = 2;
9+
public static function f() {
10+
eval(<<<'PHP'
11+
var_dump(self::FOO);
12+
var_dump(self::$bar);
13+
var_dump(self::class);
14+
var_dump(static::class);
15+
PHP
16+
);
17+
}
18+
}
19+
20+
C::f();
21+
22+
?>
23+
--EXPECT--
24+
int(1)
25+
int(2)
26+
string(1) "C"
27+
string(1) "C"
28+

Zend/zend_compile.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,11 @@ static inline zend_bool zend_is_scope_known() /* {{{ */
13251325
return 0;
13261326
}
13271327

1328+
if (!CG(active_op_array)->function_name) {
1329+
/* A file/eval will be run in the including/eval'ing scope */
1330+
return 0;
1331+
}
1332+
13281333
if (!CG(active_class_entry)) {
13291334
/* Not being in a scope is a known scope */
13301335
return 1;

0 commit comments

Comments
 (0)