Skip to content

Commit 17fa07c

Browse files
committed
finish up edge cases and tests
1 parent 8d471c7 commit 17fa07c

File tree

6 files changed

+26
-10
lines changed

6 files changed

+26
-10
lines changed

Zend/zend_API.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,21 @@ static zend_always_inline zend_result _object_and_properties_init(zval *arg, zen
18161816
return FAILURE;
18171817
}
18181818

1819+
if (class_type->required_scope) {
1820+
const zend_class_entry *scope = zend_get_executed_scope();
1821+
if (scope == NULL) {
1822+
zend_error(E_ERROR, "Cannot instantiate \"%s\" from the global scope", ZSTR_VAL(class_type->name));
1823+
}
1824+
1825+
if (class_type->required_scope_absolute) {
1826+
if (scope != class_type->required_scope && scope->lexical_scope != class_type->required_scope) {
1827+
zend_error(E_ERROR, "Cannot instantiate private \"%s\" from \"%s\"", ZSTR_VAL(class_type->name), ZSTR_VAL(scope->name));
1828+
}
1829+
} else if (!instanceof_function(scope, class_type->required_scope) && !instanceof_function(scope->lexical_scope, class_type->required_scope)) {
1830+
zend_error(E_ERROR, "Cannot instantiate protected \"%s\" from \"%s\"", ZSTR_VAL(class_type->name), ZSTR_VAL(scope->name));
1831+
}
1832+
}
1833+
18191834
if (UNEXPECTED(!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
18201835
if (UNEXPECTED(zend_update_class_constants(class_type) != SUCCESS)) {
18211836
ZVAL_NULL(arg);

Zend/zend_compile.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9170,6 +9170,12 @@ HashTable *inner_class_queue = NULL;
91709170

91719171
static void zend_defer_class_decl(zend_ast *ast)
91729172
{
9173+
ZEND_ASSERT(CG(active_class_entry));
9174+
9175+
if (CG(active_op_array)->function_name) {
9176+
zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be declared inside functions");
9177+
}
9178+
91739179
if (inner_class_queue == NULL) {
91749180
ALLOC_HASHTABLE(inner_class_queue);
91759181
zend_hash_init(inner_class_queue, 0, NULL, ZVAL_PTR_DTOR, 0);

Zend/zend_execute_API.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,21 +1217,16 @@ static zend_class_entry *zend_resolve_nested_class(zend_string *requested_name,
12171217
zend_string *shorter_scope = zend_string_init(ZSTR_VAL(scope_name), outer_len, 0);
12181218
zend_string_release(scope_name);
12191219
scope_name = shorter_scope;
1220-
1221-
if (!outer_ce) {
1222-
// we have reached the namespace/global scope so nothing to do here
1223-
break;
1224-
}
12251220
}
12261221

12271222
// handle the edge case where the class is in the global scope
1228-
if (separator == NULL) {
1223+
//if (separator == NULL) {
12291224
ce = zend_lookup_class_ex(inner_name, NULL, flags | ZEND_FETCH_CLASS_NO_INNER);
12301225
zend_string_release(scope_name);
12311226
zend_string_release(inner_name);
12321227
zend_string_release(requested_name);
12331228
return ce;
1234-
}
1229+
//}
12351230

12361231
zend_string_release(scope_name);
12371232

tests/classes/inner_classes/autoload_002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ var_dump($line);
1313
--EXPECTF--
1414
autoload(inner_classes)
1515

16-
Fatal error: Cannot access private inner class 'inner_classes\Line' in %s
16+
Fatal error: Cannot instantiate "inner_classes\Line" from the global scope in %s on line %d

tests/classes/inner_classes/return_types_002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ var_dump($outer->getInner());
2929
object(Outer\Inner)#2 (0) {
3030
}
3131

32-
Fatal error: Cannot access private inner class 'Outer\Inner' in %s on line %d
32+
Fatal error: Cannot instantiate private "Outer\Inner" from "Foo" in %s on line %d

tests/classes/inner_classes/return_types_004.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var_dump($r);
1919
test($r);
2020
?>
2121
--EXPECTF--
22-
Fatal error: Uncaught TypeError: Public method getInner cannot return private class Outer:>Inner in %s:%d
22+
Fatal error: Uncaught TypeError: Public method getInner cannot return private class Outer\Inner in %s:%d
2323
Stack trace:
2424
#0 %s(%d): Outer::getInner()
2525
#1 {main}

0 commit comments

Comments
 (0)