Skip to content

Commit f841388

Browse files
committed
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.
1 parent 3157173 commit f841388

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--TEST--
2+
Typed properties do not invoke the autoloader
3+
--FILE--
4+
<?php
5+
6+
class Test {
7+
public X $propX;
8+
public ?Y $propY;
9+
}
10+
11+
spl_autoload_register(function($class) {
12+
echo "Loading $class\n";
13+
});
14+
15+
$test = new Test;
16+
try {
17+
$test->propX = new stdClass;
18+
} catch (TypeError $e) {
19+
echo $e->getMessage(), "\n";
20+
}
21+
22+
if (true) {
23+
class X {}
24+
}
25+
26+
$test->propX = new X;
27+
var_dump($test->propX);
28+
29+
$test->propY = null;
30+
$r =& $test->propY;
31+
try {
32+
$test->propY = new stdClass;
33+
} catch (TypeError $e) {
34+
echo $e->getMessage(), "\n";
35+
}
36+
37+
if (true) {
38+
class Y {}
39+
}
40+
41+
$r = new Y;
42+
var_dump($test->propY);
43+
44+
?>
45+
--EXPECT--
46+
Typed property Test::$propX must be an instance of X, stdClass used
47+
object(X)#3 (0) {
48+
}
49+
Typed property Test::$propY must be an instance of Y or null, stdClass used
50+
object(Y)#4 (0) {
51+
}

Zend/zend_execute.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ static zend_bool zend_resolve_class_type(zend_type *type, zend_class_entry *self
941941
}
942942
ce = self_ce->parent;
943943
} else {
944-
ce = zend_lookup_class(name);
944+
ce = zend_lookup_class_ex(name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
945945
if (UNEXPECTED(!ce)) {
946946
return 0;
947947
}

0 commit comments

Comments
 (0)