1
0
mirror of https://github.com/php/php-src.git synced 2026-04-22 07:28:09 +02:00

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.
This commit is contained in:
Nikita Popov
2019-10-23 12:19:33 +02:00
parent 3157173b28
commit f841388781
2 changed files with 52 additions and 1 deletions
@@ -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) {
}
+1 -1
View File
@@ -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;
}