-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't treat failed assignment as initialization
Only reset the uninitialized property flag once the type check has succeeded. Previously the property was treated as unset rather than uninitialized after a failed assignment. Noticed this edge-case while working on accessors...
- Loading branch information
Showing
2 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
Zend/tests/type_declarations/typed_properties_failed_assign_is_not_init.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--TEST-- | ||
A failed assignment should not be considered an initialization | ||
--FILE-- | ||
<?php | ||
|
||
class Test { | ||
public int $prop; | ||
|
||
public function __get($name) { | ||
echo "__get() called\n"; | ||
return 0; | ||
} | ||
} | ||
|
||
$test = new Test; | ||
try { | ||
$test->prop; | ||
} catch (Error $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
try { | ||
$test->prop = "foo"; | ||
} catch (Error $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
try { | ||
$test->prop; | ||
} catch (Error $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Typed property Test::$prop must not be accessed before initialization | ||
Cannot assign string to property Test::$prop of type int | ||
Typed property Test::$prop must not be accessed before initialization |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters