Skip to content

Commit 0fc77cb

Browse files
committed
prevent static access
1 parent ce3299e commit 0fc77cb

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

Zend/zend_compile.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2933,7 +2933,7 @@ static void zend_compile_inner_class_ref(znode *result, zend_ast *ast, uint32_t
29332933
if (outer_class->kind == ZEND_AST_INNER_CLASS) {
29342934
zend_compile_inner_class_ref(&outer_node, outer_class, fetch_flags);
29352935
} else {
2936-
zend_compile_class_ref(&outer_node, outer_class, fetch_flags);
2936+
zend_compile_class_ref(&outer_node, outer_class, fetch_flags | ZEND_FETCH_CLASS_OUTER);
29372937
}
29382938

29392939
if (inner_class->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(inner_class)) == IS_STRING) {
@@ -2999,10 +2999,12 @@ static void zend_compile_class_ref(znode *result, zend_ast *name_ast, uint32_t f
29992999
if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
30003000
result->op_type = IS_CONST;
30013001
ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
3002+
} else if (fetch_flags & ZEND_FETCH_CLASS_OUTER && fetch_type == ZEND_FETCH_CLASS_STATIC) {
3003+
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use the static modifier on an inner class");
30023004
} else {
30033005
zend_ensure_valid_class_fetch_type(fetch_type);
30043006
result->op_type = IS_UNUSED;
3005-
result->u.op.num = fetch_type | fetch_flags;
3007+
result->u.op.num = fetch_type | (fetch_flags & ~ZEND_FETCH_CLASS_OUTER);
30063008
}
30073009
}
30083010
/* }}} */

Zend/zend_compile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,7 @@ ZEND_API zend_string *zend_type_to_string(zend_type type);
10211021
#define ZEND_FETCH_CLASS_AUTO 4
10221022
#define ZEND_FETCH_CLASS_INTERFACE 5
10231023
#define ZEND_FETCH_CLASS_TRAIT 6
1024+
#define ZEND_FETCH_CLASS_OUTER 7
10241025
#define ZEND_FETCH_CLASS_MASK 0x0f
10251026
#define ZEND_FETCH_CLASS_NO_AUTOLOAD 0x80
10261027
#define ZEND_FETCH_CLASS_SILENT 0x0100

tests/classes/inner_classes/simple_declaration_004.phpt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ scope resolution access
66
class Outer {
77
public class Middle {
88
}
9-
public static function testStatic(): self:>Middle {
10-
return new static:>Middle();
11-
}
129
public static function testSelf(): self:>Middle {
1310
return new self:>Middle();
1411
}
@@ -21,12 +18,15 @@ class Outer2 extends Outer {
2118
public static function testParent(): parent:>Middle {
2219
return new parent:>Middle();
2320
}
21+
22+
public static function testSelf(): self:>Middle {
23+
return new self:>Middle();
24+
}
2425
}
2526

26-
var_dump(Outer::testStatic());
2727
var_dump(Outer::testSelf());
2828
var_dump(Outer2::testParent());
29-
var_dump(Outer2::testStatic());
29+
var_dump(Outer2::testSelf());
3030
var_dump(Outer2::testSelf());
3131

3232
?>
@@ -35,9 +35,7 @@ object(Outer:>Middle)#1 (0) {
3535
}
3636
object(Outer:>Middle)#1 (0) {
3737
}
38-
object(Outer:>Middle)#1 (0) {
39-
}
4038
object(Outer2:>Middle)#1 (0) {
4139
}
42-
object(Outer:>Middle)#1 (0) {
40+
object(Outer2:>Middle)#1 (0) {
4341
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
static resolution
3+
--FILE--
4+
<?php
5+
6+
class Outer {
7+
class Inner {}
8+
9+
public function test() {
10+
return new static:>Inner();
11+
}
12+
}
13+
var_dump(new Outer()->test());
14+
?>
15+
--EXPECTF--
16+
Fatal error: Cannot use the static modifier on an inner class in %s on line %d

0 commit comments

Comments
 (0)