-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Typed Properties #3313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Typed Properties #3313
Changes from all commits
Commits
Show all changes
420 commits
Select commit
Hold shift + click to select a range
03861bd
Reverted incomplete fix
dstogov be071cd
Fixed test
dstogov c992fcf
A better to fix for source zval separation on type conversion caused …
dstogov f4b4051
Changed behavior of uninitialized nullable properties (accoding to co…
dstogov e657bf8
Revert useless changes
dstogov d46695d
Removed useless check (references are never fetched using FETCH_OBJ_RW)
dstogov a53b8ac
Removed duplicate code
dstogov d248c27
Added support for nullavle types in AST pretty printer
dstogov e1017f9
Revert useless change
dstogov 291d431
Added test (memory leaks)
dstogov 40aa6c2
"string" mat be also a valid "callable"
dstogov 3143597
Removed unused function and macro
dstogov 4506a70
Optimization
dstogov 05dd2e4
Removed unused zend_object_fetch_property_type_info() and renamed zen…
dstogov 023df77
Fixed wrong exception when passing element of typed property by refer…
dstogov 56e2aaa
Optimisation
dstogov d7f313e
Added test (failed with valgrind)
dstogov 892994d
Added test (failed with valgrind)
dstogov b0dcf0c
Fixed memory leak
dstogov 2019f66
Check type after constant resolution
dstogov c304c6b
Support for type checks of named constants in property default values.
dstogov b2cebf6
Fixed memory leaks
dstogov 8bc2078
Faster +=/++/--
dstogov 26bf822
Merge branch 'master' into typed-properties
dstogov 865686f
Fixed ZTS compatibility
dstogov bf875fb
Fixed compilation warning
dstogov fc41cdc
fixed white-spaces
dstogov 8beea3d
Optimization
dstogov 13c86d9
Cleanup ext/reflection changes
dstogov 697b1b1
Moved BG(unserialize_refs) into php_unserialize_data_t
dstogov c5e45ff
Keep GCC from incorrect over-optimisation
dstogov 6bb1fa9
Removed useles code (default value should be already properly set in …
dstogov 5811e86
Don't mess NULL and UNDEF default value back and force. Set proper va…
dstogov 037da58
Remove duplicate checks
dstogov 03e7112
Fixed incorrect argument types
dstogov cc19dc5
Remove bad checks for simple property inheritance
bwoebi 3bbe52d
Merge branch 'master' of https://github.com/php/php-src into typed-pr…
krakjoe de84f50
changes prompted by review
krakjoe 71cda1b
make consistent with RFC (nullables)
krakjoe 8eec27f
fix inheritance error
krakjoe 67ffff9
Merge branch 'master' into typed_properties
bwoebi 1b8bd88
Initial ref types patch
bwoebi 0efa68b
Fix make test
bwoebi a75e03a
Take properly care of cases where the by-ref value is *consumed* first
bwoebi 433e637
Remove all the nice code making it actually worse again ...
bwoebi f4ea705
Use reftype, not its location...
bwoebi ad85b55
few changes for bob to check
krakjoe c347d8d
Merge branch 'typed_ref_properties' of https://github.com/bwoebi/php-…
krakjoe 2769866
more zpp changes
krakjoe 8d3a9f6
finish pcntl, probably
krakjoe d7c7b9c
some of sockets
krakjoe cd32f63
more zpppppp stuff
krakjoe a0aaeed
use macros that actually exist, rather than nonsense
krakjoe 93707d7
Fix a ton of bugs and tests
bwoebi bb230fd
Add more guards for typed by-ref assign + fixes
bwoebi 38583b9
Fix a few more tests
bwoebi fea2aa8
typo; s/zend_get_long/zval_get_long/
bwoebi d9b964e
Fix iterator_035.phpt and pcntl/003.phpt
bwoebi 0cc95b8
Remove lchown($file, -5); test
bwoebi 694752b
Merge branch 'master' into typed_ref_properties
bwoebi d5b3bd8
Add support for by-ref foreach
bwoebi 0bf02dc
Separate static property assignment out into dedicated assign ops
bwoebi 4c57735
Merge remote-tracking branch 'origin' into typed_ref_properties
bwoebi 786d4bb
Fixed optimizer support for ZEND_ASSIGN_OBJ_REF
dstogov a9b0cd7
Implemented type inference support for typed properties.
dstogov 41d3882
Merge branch 'master' into typed_ref_properties
bwoebi 3e9d02e
Merge remote-tracking branch 'github/typed_ref_properties' into typed…
bwoebi f56bd39
Fixed tests (made them 32/64-bit independent)
dstogov 354c1af
Fix a bunch of bugs related to e8109e09
bwoebi 69c7210
Merge branch 'master' into typed_ref_properties
dstogov f2872c9
Merge branch 'master' of https://github.com/php/php-src into typed_re…
bwoebi e20a1a3
Fix crash in pcntl_wait()
bwoebi b021ec8
Merge branch 'master' into typed_ref_properties
dstogov 629521e
Fix constant value overwrite
bwoebi 78dae20
Add support for iterable typed properties
bwoebi bafcfc0
Merge branch 'master' into typed_ref_properties
nikic 965f2c8
Fix build failures
nikic f42495d
Initial ref type in MAKE_REF_EX
nikic ed00f11
Solve the clashing FETCH_REF|CACHE_SLOT
bwoebi 6709fbb
Fix few tests
bwoebi f55663f
Fix cache_slot of ASSIGN_OBJ_REF
bwoebi 6d0c443
Opcache fixes
bwoebi c453c98
Fix build failure in exif
bwoebi 64a309f
Add one more test for iterables by ref
bwoebi 98019a9
Fix foreach object iteration with holes
bwoebi e74f89c
Merge branch 'master' into typed_ref_properties
nikic 83ec0df
Restore old code for post_incdec as well
nikic 7f59681
Adjust test with new behavior
nikic 1b0bc33
Fix compiler warning
nikic fcba7fe
Merge branch 'master' into typed_ref_properties
nikic 2e28eb6
Improve default value handling
nikic 4473554
Allow int default value for float properties
nikic 4ef0626
Use not_in_object_context helper
nikic 14081d8
Merge branch 'master' into typed_ref_properties
nikic cb75a25
Fix ASSIGN_OBJ_REF implementation
nikic 3021bbc
Don't automatically initialize nullable prop to null
nikic 230142e
Forbid callable type hint in typed properties
nikic b3da284
Merge branch 'extra_static_prop_assign_ops' into typed_ref_properties
bwoebi b707745
Merge branch 'typed_ref_properties' of https://github.com/bwoebi/php-…
bwoebi 759d078
Add support for typed static properties (no opcache yet)
bwoebi ea58600
Fix assign to typed ref with weak type conversion
bwoebi 149fd4b
Fix opcache for typed static properties
bwoebi bb0e169
Fix remaining tests
bwoebi ddfa19d
Fixing conversion with __get() on uninit typed properties
bwoebi 8e23778
Prevent constant propagation into ASSIGN_*_REF OP_DATA
bwoebi fffb439
Add ZEND_PRE/POST_INC/DEC_STATIC_PROP
bwoebi 17bdcef
Property compute intersection types for $obj->prop =& $ref
nikic 40079bf
Add missing test file...
nikic bc900ea
Fix leak
nikic a450c28
$a->b = $c must return (string) $c if $a->b is of type string
bwoebi ebb64ae
Drop overeager assert
nikic 7c96afa
Enforce reference checks on ReflectionClass::setStaticPropertyValue()…
bwoebi 10ac526
Use coercive mode for setStaticPropertyValue()
bwoebi 452d0a7
Keep track of reference types
bwoebi 0023518
Fix memory leaks & stupid bugs introduced in last commit
bwoebi c69f8d0
Fix static property ref assignment without types
bwoebi f00a4df
extract() must check typed refs when overwriting
bwoebi 1228d70
Mark subpats as ARRAY_ASSIGNABLE
nikic 440a78a
Use TRY_ASSIGN_LONG for zcount parameters
nikic c6f5f13
Fix refcounting of zend_try_assign()
bwoebi df4460e
Merge branch 'master' into typed_ref_properties
nikic 693a603
Enforce reference type in settype()
nikic 2d47e84
Make ZEND_TRY_ASSIGN_* implementation less magic
nikic f6ad85f
Unmangle property name before printing
nikic 898cd3d
Fix reference type tracking on clone
bwoebi 5ce6ebc
Merge branch 'master' of https://github.com/php/php-src into typed_re…
bwoebi 767b2e5
Update extensions for ZEND_TRY_ASSIGN_*
nikic 13eba68
Merge branch 'master' into typed_ref_properties
nikic a3f60c0
Opcache handling fixes and improvements
nikic a19b2cc
Merge remote-tracking branch 'php-src/master' into typed_ref_properties
nikic 062de9d
Reduce ASSIGN_STATIC_PROP_REF specialization
nikic c91ea15
Accept BP type in get_static_property
nikic 50b1cf3
Prevent type violations via array auto-promotion
bwoebi 4ce995d
Fix type check for array promotion check
bwoebi dc88db9
Merge branch 'master' into typed_ref_properties
nikic 406d594
Restore fast-path check for uninitialized static property
nikic 561f868
Add ReflectionProperty::isInitialized()
bwoebi 157a09d
Merge branch 'master' into typed_ref_properties
nikic 04aaea8
Minor cleanups
nikic e8dc511
Fix refcount on std_write_property with coercion
bwoebi ef67def
Fix ReflectionProperty::isInitialized() for dynamic props
nikic 050f642
Fix ReflectionProperty::setValue() on typed static property
nikic 7d3768b
Merge branch 'master' into typed_ref_properties
nikic 8355232
Fix class check
nikic 13777bd
References are the intersection of all present property types at any …
bwoebi 428a00f
Fix a last test
bwoebi e888cf1
Remove 't' zpp type
nikic d35b338
Drop ARRAY_ASSIGNABLE as well
nikic dee7710
Merge branch 'master' into typed_ref_properties
nikic 3bfbbf8
Abort early on exception in extract()
nikic acd013c
Move throwing of error into verify_ref_assignable_zval()
nikic a10222e
Don't constrain type intersections
nikic 2eadd9e
Merge branch 'master' of https://github.com/php/php-src into typed_re…
bwoebi ae8a198
Fix build without execute_data $register
bwoebi eda393b
Move push/pop function prologue off fast path in VM
bwoebi 1f73729
Resolve aliases during property type LSP checks
nikic 7f25e95
Check for conflicting property types in traits
nikic 398027b
Fix write_property handlers and add zend_std_get_property_with_info
bwoebi e7b86fb
Remove *tmp arg from zend_verify_property_type()
bwoebi 92302a8
Remove useless separations/copies
bwoebi ffbf025
handle by ref mysqli/PDO statement result binding
bwoebi 5e2ebdc
Fix string length updates
bwoebi 5c94955
Merge branch 'master' into typed_ref_properties
bwoebi ae3e788
Fix a last function to use zend_try_assign()
bwoebi 872084b
Fix non-string key usage with typed properties
bwoebi c6310fb
Merge remote-tracking branch 'origin/master' into typed_ref_properties
bwoebi a7fc684
Merge remote-tracking branch 'origin/master' into typed_ref_properties
bwoebi 4f2350e
Merge remote-tracking branch 'php-src/master' into typed_ref_properties
nikic 0d579d0
Add test for typed properties with integer keys
bwoebi 5ba37d8
Remove unnecessary EXPECTFs
nikic aa9a8dd
One more EXPECTF case
nikic 8e0380b
Use seen_prop when reporting weak type error
nikic f1a2522
Add test for settype() on typed reference
nikic 1f235d3
Merge branch 'master' into typed_ref_properties
nikic 8df6714
Minor test improvements
nikic 96ea906
Drop redundant "Typed" prefix from callable/void error
nikic 494585f
Restore old zend_binary_assign_op_obj_dim code
nikic e8cf005
Revert some unnecessary changes
nikic c44eba9
Change zend_declare_typed_property to accept zend_type
bwoebi 7424833
Fix parent private property type affecting child class
bwoebi aa769fb
Remove redundant ISUNDEF checks in mysqlnd
bwoebi eff6b11
Handle NULL result in assign_ref
nikic 1147467
Fix DateInterval write_property handler
nikic 236a72f
Cleanup tests and improve a few error messages
bwoebi f697a7b
Fix two more tests in ext/standard
bwoebi f2d72b9
Evaluate expressions involving constants in property initializers acc…
bwoebi c78d5f6
Make visibility a property of the prop_group AST node
bwoebi ce68539
Enforce strict types for property initializer evaluation
bwoebi 68abea6
Remove REF flag from ASSIGN_(OBJ|STATIC_PROP)_REF
nikic e1508f2
Add a missing ASSIGN_STATIC_PROP_REF case
nikic 64c7ca8
Merge branch 'master' into typed_ref_properties
nikic fe4f9f3
Don't return pointer to stack memory
nikic ccf4481
Initialize return value for one exception case
nikic 7a91259
Merge branch 'master' into typed_ref_properties
nikic 313590d
Correctly handle resolved types in reflection
nikic 98b8e8e
Remove unused enum
nikic 6f9eac9
Only lookup prop info for INDIRECT
nikic b8881f0
Fix usage of self typehint in static trait properties
bwoebi de9a77f
Fix typed unint static property taken by ref being undefined
bwoebi f7e6099
Merge branch 'master' into typed_ref_properties
nikic 4ca4996
Fix reflection after recent master changes
nikic 3e7ecb3
Merge remote-tracking branch 'php-src/master' into typed_ref_properties
nikic 23b1d0a
Simplify code using compile_typename
nikic f1e09b3
Fix tests for master changes
nikic 02f3baa
Fix cache slot invalidation
nikic c38032a
Prevent any incdec on integer boundaries with typed properties
bwoebi 1d555b7
Fix leak when modifying typed property during assignment
bwoebi db13ac3
Use zend_type_error() instead of zend_throw_exception_ex()
bwoebi a119939
Fix unsetting shadowed typed properties holding a reference
bwoebi f6209ee
Make all uninit access Error instead of TypeError & properly unmangle…
bwoebi 5ae9a3f
Merge branch 'master' into typed_ref_properties
dstogov 911e7c6
Merge branch 'master' into typed_ref_properties
dstogov d274d58
Fix test
bwoebi d34cddc
Merge branch 'master' into typed_ref_properties
nikic 10151f1
Merge remote-tracking branch 'php-src/master' into typed_ref_properties
nikic e231c31
Some opcache fixes
nikic 18fe3b0
Merge branch 'master' into typed_ref_properties
nikic 21e405a
Initialize unserialize_data refs
nikic 6a27224
Check for type violations through implicit object init
nikic 25a517c
Handle the reference case as well
nikic cca6c1c
Remove incorrect string check, add tests
nikic 373579c
Check for no type in "type_assignable" functions
nikic 834c8d0
Merge branch 'master' into typed_ref_properties
nikic 92cfec9
Improve auto-initialization related error messages
nikic ac1ac47
Extract auto-init errors into cold helpers
nikic 0311fc5
Remove hardcoded path in test
nikic 6574e7f
Handle all FETCH_OBJ flags in opcache
nikic d44a513
Fix static prop ref sources memory leak under opcache
nikic f95ef9c
Fix fastcall use on Windows
nikic c0f7ab6
Test tweaks
nikic 3cdaf71
Remove unnecessary flag handling in compact_literals
nikic eec91a2
Remove unnecessary result undefing
nikic 4efefc3
Add ASSIGN_STATIC_PROP to opcodes that can have unused result
nikic f6257b7
Pack obj fetch flags tighter, so it's compatible with 32 bit
nikic f28943c
Start working on inference support for typed static props
nikic 4910f1d
Support type inference for static properties
nikic 78acbd7
Don't call magic in ReflectionProperty::isInitialized()
nikic 7430115
Remove redundant check in zend_inheritance
nikic c5d916f
Remove dead code
nikic 8b71442
Convert if/else to switch
nikic 401ff4c
Revert "Remove lchown($file, -5); test"
nikic dbf7d82
Allow references to typed properties during unserialization
nikic b1df1b7
Fix type checks during unserialization
nikic bac5425
Correctly handle refs on ASSIGN_OBJ fastpath
nikic 882aaba
Handle ASSIGN_STATIC_PROP compound assign in SCCP
nikic 9788344
Remove bogus check in ASSIGN_OBJ_REF
nikic c89b5ba
Add slot to prop info mapping mechanism
nikic 40540b7
Fix ref source removal on object destruction
nikic 9360955
Assert that all type sources have been removed when ref is dtored
nikic 5f7d376
Use get_property_info_for slot while unsetting properties as well
nikic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,45 @@ | ||
--TEST-- | ||
Test typed properties basic operation | ||
--FILE-- | ||
<?php | ||
var_dump(new class(1, 2.2, true, ["four"], new stdClass) { | ||
public int $int; | ||
public float $float; | ||
public bool $bool; | ||
public array $array; | ||
public stdClass $std; | ||
public iterable $it; | ||
|
||
public function __construct(int $int, float $float, bool $bool, array $array, stdClass $std) { | ||
$this->int = $int; | ||
$this->float = $float; | ||
$this->bool = $bool; | ||
$this->array = $array; | ||
$this->std = $std; | ||
$this->it = $array; | ||
} | ||
}); | ||
?> | ||
--EXPECTF-- | ||
object(class@anonymous)#%d (6) { | ||
["int"]=> | ||
int(1) | ||
["float"]=> | ||
float(2.2) | ||
["bool"]=> | ||
bool(true) | ||
["array"]=> | ||
array(1) { | ||
[0]=> | ||
string(4) "four" | ||
} | ||
["std"]=> | ||
object(stdClass)#%d (0) { | ||
} | ||
["it"]=> | ||
array(1) { | ||
[0]=> | ||
string(4) "four" | ||
} | ||
} | ||
|
This file contains hidden or 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,17 @@ | ||
--TEST-- | ||
Test typed properties error condition (read uninitialized) | ||
--FILE-- | ||
<?php | ||
$thing = new class() { | ||
public int $int; | ||
}; | ||
|
||
var_dump($thing->int); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught Error: Typed property class@anonymous::$int must not be accessed before initialization in %s:6 | ||
Stack trace: | ||
#0 {main} | ||
thrown in %s on line 6 | ||
|
||
|
This file contains hidden or 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,16 @@ | ||
--TEST-- | ||
Test typed properties error condition (fetch uninitialized by reference) | ||
--FILE-- | ||
<?php | ||
$thing = new class() { | ||
public int $int; | ||
}; | ||
|
||
$var = &$thing->int; | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught Error: Cannot access uninitialized non-nullable property class@anonymous::$int by reference in %s:%d | ||
Stack trace: | ||
#0 {main} | ||
thrown in %s on line %d | ||
|
This file contains hidden or 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,20 @@ | ||
--TEST-- | ||
Test typed properties error condition (type mismatch) | ||
--FILE-- | ||
<?php | ||
new class("PHP 7 is better than you, and it knows it ...") { | ||
public int $int; | ||
|
||
public function __construct(string $string) { | ||
$this->int = $string; | ||
} | ||
}; | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught TypeError: Typed property class@anonymous::$int must be int, string used in %s:6 | ||
Stack trace: | ||
#0 %s(2): class@anonymous->__construct('PHP 7 is better...') | ||
#1 {main} | ||
thrown in %s on line 6 | ||
|
||
|
This file contains hidden or 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,20 @@ | ||
--TEST-- | ||
Test typed properties error condition (type mismatch object) | ||
--FILE-- | ||
<?php | ||
class Dummy {} | ||
|
||
new class(new Dummy) { | ||
public stdClass $std; | ||
|
||
public function __construct(Dummy $dummy) { | ||
$this->std = $dummy; | ||
} | ||
}; | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught TypeError: Typed property class@anonymous::$std must be an instance of stdClass, Dummy used in %s:8 | ||
Stack trace: | ||
#0 %s(4): class@anonymous->__construct(Object(Dummy)) | ||
#1 {main} | ||
thrown in %s on line 8 |
This file contains hidden or 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,15 @@ | ||
--TEST-- | ||
Test typed properties inheritance (scalar) | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public int $qux; | ||
} | ||
|
||
class Bar extends Foo { | ||
public string $qux; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Type of Bar::$qux must be int (as in class Foo) in %s on line 8 | ||
|
This file contains hidden or 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,19 @@ | ||
--TEST-- | ||
Test typed properties inheritance | ||
--FILE-- | ||
<?php | ||
class Whatever {} | ||
class Thing extends Whatever {} | ||
|
||
class Foo { | ||
public Whatever $qux; | ||
} | ||
|
||
class Bar extends Foo { | ||
public Thing $qux; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Type of Bar::$qux must be Whatever (as in class Foo) in %s on line 11 | ||
|
||
|
This file contains hidden or 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,16 @@ | ||
--TEST-- | ||
Test typed properties inheritance (missing info) | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public int $qux; | ||
} | ||
|
||
class Bar extends Foo { | ||
public $qux; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Type of Bar::$qux must be int (as in class Foo) in %s on line 8 | ||
|
||
|
This file contains hidden or 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,23 @@ | ||
--TEST-- | ||
Test typed properties unset leaves properties in an uninitialized state | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public int $bar; | ||
|
||
public function __get($name) { | ||
var_dump($name); | ||
/* return value has to be compatible with int */ | ||
return 0; | ||
} | ||
} | ||
|
||
$foo = new Foo(); | ||
|
||
unset($foo->bar); | ||
|
||
var_dump($foo->bar); | ||
?> | ||
--EXPECT-- | ||
string(3) "bar" | ||
int(0) |
This file contains hidden or 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,17 @@ | ||
--TEST-- | ||
Test typed properties allow fetch reference | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public int $bar = 1; | ||
} | ||
|
||
$cb = function(int &$bar) { | ||
var_dump($bar); | ||
}; | ||
|
||
$foo = new Foo(); | ||
$cb($foo->bar); | ||
?> | ||
--EXPECT-- | ||
int(1) |
This file contains hidden or 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,18 @@ | ||
--TEST-- | ||
Test typed properties allow fetch reference for init array | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public int $bar = 1; | ||
} | ||
|
||
$foo = new Foo(); | ||
|
||
$array = [&$foo->bar]; | ||
var_dump($array); | ||
?> | ||
--EXPECT-- | ||
array(1) { | ||
[0]=> | ||
&int(1) | ||
} |
This file contains hidden or 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,19 @@ | ||
--TEST-- | ||
Test typed properties allow fetch reference for foreach | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public int $bar = 1; | ||
} | ||
|
||
$foo = new Foo(); | ||
foreach ($foo as &$prop) { | ||
$prop++; | ||
} | ||
var_dump($foo); | ||
?> | ||
--EXPECT-- | ||
object(Foo)#1 (1) { | ||
["bar"]=> | ||
&int(2) | ||
} |
This file contains hidden or 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,10 @@ | ||
--TEST-- | ||
Test typed properties disallow incorrect type initial value (scalar) | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public int $bar = "string"; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Default value for property of type int can only be int in %s on line 3 |
This file contains hidden or 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,10 @@ | ||
--TEST-- | ||
Test typed properties disallow incorrect type initial value (array) | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public array $bar = 32; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Default value for property of type array can only be an array in %s on line 3 |
This file contains hidden or 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,10 @@ | ||
--TEST-- | ||
Test typed properties disallow incorrect type initial value (object) | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public stdClass $bar = null; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Default value for property of type stdClass may not be null. Use the nullable type ?stdClass to allow null default value in %s on line %d |
This file contains hidden or 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,19 @@ | ||
--TEST-- | ||
Test typed properties initial values | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public int $int = 1; | ||
public float $flt = 2.2; | ||
public float $flt2 = 2; | ||
public array $arr = []; | ||
public bool $bool = false; | ||
public iterable $iter = []; | ||
} | ||
echo "ok\n"; | ||
?> | ||
--EXPECT-- | ||
ok | ||
|
||
|
||
|
This file contains hidden or 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,12 @@ | ||
--TEST-- | ||
Test typed properties disallow void | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public void $int; | ||
} | ||
|
||
$foo = new Foo(); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Property Foo::$int cannot have type void in %s on line 3 |
This file contains hidden or 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,17 @@ | ||
--TEST-- | ||
Test typed properties type applies to all props in group | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public int $bar, | ||
$qux; | ||
} | ||
|
||
$reflector = new ReflectionClass(Foo::class); | ||
|
||
$prop = $reflector->getProperty("qux"); | ||
|
||
var_dump((string) $prop->getType()); | ||
?> | ||
--EXPECT-- | ||
string(3) "int" |
This file contains hidden or 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,27 @@ | ||
--TEST-- | ||
Test typed properties int must not be allowed to overflow | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public int $bar = PHP_INT_MAX; | ||
|
||
public function inc() { | ||
return ++$this->bar; | ||
} | ||
} | ||
|
||
$foo = new Foo(); | ||
|
||
var_dump($foo->inc()); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught TypeError: Typed property Foo::$bar must be int, float used in %s:6 | ||
Stack trace: | ||
#0 %s(12): Foo->inc() | ||
#1 {main} | ||
thrown in %s on line 6 | ||
|
||
|
||
|
||
|
||
|
This file contains hidden or 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,26 @@ | ||
--TEST-- | ||
Test typed properties binary assign op helper test | ||
--FILE-- | ||
<?php | ||
declare(strict_types=1); | ||
|
||
class Foo { | ||
public int $bar = 0; | ||
|
||
public function __construct() { | ||
$this->bar += 2; | ||
try { | ||
$this->bar += 1.5; | ||
} catch (TypeError $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
} | ||
} | ||
|
||
$foo = new Foo(); | ||
|
||
var_dump($foo->bar); | ||
?> | ||
--EXPECT-- | ||
Typed property Foo::$bar must be int, float used | ||
int(2) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.