com php-src: Allow self etc in eval / file scope: Zend/tests/se lf_class_const_outside_class.phpt Zend/tests/self _in_eval.phpt Zend/zend_compile.c
| From: | Nikita Popov | Date: | Thu, 07 May 2015 13:38:16 +0000 |
| Subject: | com php-src: Allow self etc in eval / file scope: Zend/tests/se lf_class_const_outside_class.phpt Zend/tests/self _in_eval.phpt Zend/zend_compile.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-86645@lists.php.net to get a copy of this message | ||
Commit: 6ef8ae65a2f8f973d419c193aa4a1e6150f3646a
Author: Nikita Popov <nikic@php.net> Thu, 7 May 2015 15:17:37 +0200
Parents: ab4ccffc4cceb877d2ce3144e5c499bd651886bf
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=6ef8ae65a2f8f973d419c193aa4a1e6150f3646a
Log:
Allow self etc in eval / file scope
This fixes a regression from 16a9bc1ec20533c76ba992bfc64dd69e7b7d9001.
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.
Changed paths:
M Zend/tests/self_class_const_outside_class.phpt
A Zend/tests/self_in_eval.phpt
M Zend/zend_compile.c
Diff:
diff --git a/Zend/tests/self_class_const_outside_class.phpt
b/Zend/tests/self_class_const_outside_class.phpt
index 541f3da..7a1f989 100644
--- a/Zend/tests/self_class_const_outside_class.phpt
+++ b/Zend/tests/self_class_const_outside_class.phpt
@@ -1,8 +1,10 @@
--TEST--
-Accessing self::FOO outside a class
+Accessing self::FOO in a free function
--FILE--
<?php
-var_dump(self::FOO);
+function test() {
+ var_dump(self::FOO);
+}
?>
--EXPECTF--
Fatal error: Cannot use "self" when no class scope is active in %s on line %d
diff --git a/Zend/tests/self_in_eval.phpt b/Zend/tests/self_in_eval.phpt
new file mode 100644
index 0000000..41c2654
--- /dev/null
+++ b/Zend/tests/self_in_eval.phpt
@@ -0,0 +1,28 @@
+--TEST--
+self etc. can be used in eval() in a class scope
+--FILE--
+<?php
+
+class C {
+ const FOO = 1;
+ private static $bar = 2;
+ public static function f() {
+ eval(<<<'PHP'
+var_dump(self::FOO);
+var_dump(self::$bar);
+var_dump(self::class);
+var_dump(static::class);
+PHP
+ );
+ }
+}
+
+C::f();
+
+?>
+--EXPECT--
+int(1)
+int(2)
+string(1) "C"
+string(1) "C"
+
diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index a543821..2606304 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -1325,6 +1325,11 @@ static inline zend_bool zend_is_scope_known() /* {{{ */
return 0;
}
+ if (!CG(active_op_array)->function_name) {
+ /* A file/eval will be run in the including/eval'ing scope */
+ return 0;
+ }
+
if (!CG(active_class_entry)) {
/* Not being in a scope is a known scope */
return 1;