Skip to content

Commit 278ee78

Browse files
committed
Zend: add zend_cannot_instantiate_class()
1 parent eec8328 commit 278ee78

4 files changed

Lines changed: 19 additions & 15 deletions

File tree

‎Zend/zend_API.c‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,16 +1873,7 @@ ZEND_API void object_properties_load(zend_object *object, const HashTable *prope
18731873
static zend_always_inline zend_result _object_and_properties_init(zval *arg, zend_class_entry *ce, HashTable *properties) /* {{{ */
18741874
{
18751875
if (UNEXPECTED(ce->ce_flags & ZEND_ACC_UNINSTANTIABLE)) {
1876-
if (ce->ce_flags & ZEND_ACC_INTERFACE) {
1877-
zend_throw_error(NULL, "Cannot instantiate interface %s", ZSTR_VAL(ce->name));
1878-
} else if (ce->ce_flags & ZEND_ACC_TRAIT) {
1879-
zend_throw_error(NULL, "Cannot instantiate trait %s", ZSTR_VAL(ce->name));
1880-
} else if (ce->ce_flags & ZEND_ACC_ENUM) {
1881-
zend_throw_error(NULL, "Cannot instantiate enum %s", ZSTR_VAL(ce->name));
1882-
} else {
1883-
ZEND_ASSERT(ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS));
1884-
zend_throw_error(NULL, "Cannot instantiate abstract class %s", ZSTR_VAL(ce->name));
1885-
}
1876+
zend_cannot_instantiate_class(ce);
18861877
ZVAL_NULL(arg);
18871878
Z_OBJ_P(arg) = NULL;
18881879
return FAILURE;

‎Zend/zend_lazy_objects.c‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,7 @@ ZEND_API zend_object *zend_object_make_lazy(zend_object *obj,
260260

261261
if (!obj) {
262262
if (UNEXPECTED(reflection_ce->ce_flags & ZEND_ACC_UNINSTANTIABLE)) {
263-
zval zobj;
264-
/* Call object_init_ex() for the generated exception */
265-
zend_result result = object_init_ex(&zobj, reflection_ce);
266-
ZEND_ASSERT(result == FAILURE && EG(exception));
267-
(void)result;
263+
zend_cannot_instantiate_class(reflection_ce);
268264
return NULL;
269265
}
270266

‎Zend/zend_objects_API.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,18 @@ ZEND_API ZEND_COLD zend_property_info *zend_get_property_info_for_slot_slow(zend
211211
} ZEND_HASH_FOREACH_END();
212212
return NULL;
213213
}
214+
215+
ZEND_API ZEND_COLD zend_never_inline void zend_cannot_instantiate_class(
216+
const zend_class_entry *ce
217+
) {
218+
ZEND_ASSERT(ce->ce_flags & ZEND_ACC_UNINSTANTIABLE);
219+
if (ce->ce_flags & ZEND_ACC_INTERFACE) {
220+
zend_throw_error(NULL, "Cannot instantiate interface %s", ZSTR_VAL(ce->name));
221+
} else if (ce->ce_flags & ZEND_ACC_TRAIT) {
222+
zend_throw_error(NULL, "Cannot instantiate trait %s", ZSTR_VAL(ce->name));
223+
} else if (ce->ce_flags & ZEND_ACC_ENUM) {
224+
zend_throw_error(NULL, "Cannot instantiate enum %s", ZSTR_VAL(ce->name));
225+
} else if (ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
226+
zend_throw_error(NULL, "Cannot instantiate abstract class %s", ZSTR_VAL(ce->name));
227+
}
228+
}

‎Zend/zend_objects_API.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,6 @@ static zend_always_inline bool zend_check_method_accessible(const zend_function
160160
return true;
161161
}
162162

163+
ZEND_API ZEND_COLD zend_never_inline void zend_cannot_instantiate_class(const zend_class_entry *ce);
164+
163165
#endif /* ZEND_OBJECTS_H */

0 commit comments

Comments
 (0)