com php-src: Don't autoload when checking property types: Zend/tests/type_declarations/typed_properties_class_loa ding.phpt Zend/zend_execute.c

From: Date: Wed, 23 Oct 2019 10:21:30 +0000
Subject: com php-src: Don't autoload when checking property types: Zend/tests/type_declarations/typed_properties_class_loa ding.phpt Zend/zend_execute.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-109028@lists.php.net to get a copy of this message
Commit: f8413887816c4cecda75e6ae59dc3d1a74bb4f77 Author: Nikita Popov <nikita.ppv@gmail.com> Wed, 23 Oct 2019 12:19:33 +0200 Parents: 3157173b28735ebd8f1bd620c8672558d7b70c74 Branches: PHP-7.4 master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=f8413887816c4cecda75e6ae59dc3d1a74bb4f77 Log: Don't autoload when checking property types Noticed while working on union types: We do not load argument and return types during type checks, but we do load property types. I'm normalizing the behavior towards the existing status quo (not loading), though we may consider loading everywhere (all types, and instanceof) in order to properly support class aliases. Changed paths: A Zend/tests/type_declarations/typed_properties_class_loading.phpt M Zend/zend_execute.c Diff: diff --git a/Zend/tests/type_declarations/typed_properties_class_loading.phpt b/Zend/tests/type_declarations/typed_properties_class_loading.phpt new file mode 100644 index 00000000000..0612624454b --- /dev/null +++ b/Zend/tests/type_declarations/typed_properties_class_loading.phpt @@ -0,0 +1,51 @@ +--TEST-- +Typed properties do not invoke the autoloader +--FILE-- +<?php + +class Test { + public X $propX; + public ?Y $propY; +} + +spl_autoload_register(function($class) { + echo "Loading $class\n"; +}); + +$test = new Test; +try { + $test->propX = new stdClass; +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +if (true) { + class X {} +} + +$test->propX = new X; +var_dump($test->propX); + +$test->propY = null; +$r =& $test->propY; +try { + $test->propY = new stdClass; +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +if (true) { + class Y {} +} + +$r = new Y; +var_dump($test->propY); + +?> +--EXPECT-- +Typed property Test::$propX must be an instance of X, stdClass used +object(X)#3 (0) { +} +Typed property Test::$propY must be an instance of Y or null, stdClass used +object(Y)#4 (0) { +} diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index 04aa3fee6e6..5407d896808 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -941,7 +941,7 @@ static zend_bool zend_resolve_class_type(zend_type *type, zend_class_entry *self } ce = self_ce->parent; } else { - ce = zend_lookup_class(name); + ce = zend_lookup_class_ex(name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD); if (UNEXPECTED(!ce)) { return 0; }

« previous php.cvs (#109028) next »