Skip to content

Commit 99c5e08

Browse files
committed
Throw Error instead of E_ERROR when calling method on incomplete class
There's no reason for this to abort execution completely.
1 parent 4ce3830 commit 99c5e08

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

ext/standard/incomplete_class.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,24 @@
2929
PHPAPI zend_class_entry *php_ce_incomplete_class;
3030
static zend_object_handlers php_incomplete_object_handlers;
3131

32-
/* {{{ incomplete_class_message */
3332
static void incomplete_class_message(zend_object *object, int error_type)
3433
{
35-
zend_string *class_name;
36-
37-
class_name = php_lookup_class_name(object);
34+
zend_string *class_name = php_lookup_class_name(object);
35+
php_error_docref(NULL, error_type, INCOMPLETE_CLASS_MSG,
36+
class_name ? ZSTR_VAL(class_name) : "unknown");
37+
if (class_name) {
38+
zend_string_release_ex(class_name, 0);
39+
}
40+
}
3841

42+
static void throw_incomplete_class_error(zend_object *object)
43+
{
44+
zend_string *class_name = php_lookup_class_name(object);
45+
zend_throw_error(NULL, INCOMPLETE_CLASS_MSG, class_name ? ZSTR_VAL(class_name) : "unknown");
3946
if (class_name) {
40-
php_error_docref(NULL, error_type, INCOMPLETE_CLASS_MSG, ZSTR_VAL(class_name));
4147
zend_string_release_ex(class_name, 0);
42-
} else {
43-
php_error_docref(NULL, error_type, INCOMPLETE_CLASS_MSG, "unknown");
4448
}
4549
}
46-
/* }}} */
4750

4851
static zval *incomplete_class_get_property(zend_object *object, zend_string *member, int type, void **cache_slot, zval *rv) /* {{{ */
4952
{
@@ -87,7 +90,7 @@ static int incomplete_class_has_property(zend_object *object, zend_string *membe
8790

8891
static zend_function *incomplete_class_get_method(zend_object **object, zend_string *method, const zval *key) /* {{{ */
8992
{
90-
incomplete_class_message(*object, E_ERROR);
93+
throw_incomplete_class_error(*object);
9194
return NULL;
9295
}
9396
/* }}} */

ext/standard/tests/serialize/serialization_objects_005.phpt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ var_dump(isset($incomplete->x));
4848
unset($incomplete->x);
4949
var_dump($incomplete->x);
5050

51-
$incomplete->f();
51+
try {
52+
$incomplete->f();
53+
} catch (Error $e) {
54+
echo $e->getMessage(), "\n";
55+
}
5256

5357
echo "Done";
5458
?>
@@ -105,5 +109,5 @@ Notice: main(): The script tried to execute a method or access a property of an
105109

106110
Notice: main(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "C" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide an autoloader to load the class definition in %s on line %d
107111
NULL
108-
109-
Fatal error: main(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "C" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide an autoloader to load the class definition in %s on line %d
112+
The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "C" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide an autoloader to load the class definition
113+
Done

0 commit comments

Comments
 (0)