Skip to content

Typed Properties #1797

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
wants to merge 210 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
210 commits
Select commit Hold shift + click to select a range
d5e097e
initial import
krakjoe Mar 5, 2016
3b9c9ff
todos and whatever
krakjoe Mar 6, 2016
e016859
remove note
krakjoe Mar 6, 2016
8f74635
exception on uninitialized property access
krakjoe Mar 6, 2016
70e0e4d
be tidy, don't leak, use flags ... other things, probably
krakjoe Mar 6, 2016
5a07809
some tests ...
krakjoe Mar 6, 2016
9303e6d
fixed todo
krakjoe Mar 6, 2016
b3e15ca
set flags correctly, remove todo
krakjoe Mar 6, 2016
3dfb8c2
unused var
krakjoe Mar 6, 2016
8c51aac
another test
krakjoe Mar 6, 2016
37e169c
rename function so it makes sense
krakjoe Mar 6, 2016
149cb01
use flags here
krakjoe Mar 7, 2016
52b184e
unexpected
krakjoe Mar 7, 2016
bd4368c
more words in error message
krakjoe Mar 7, 2016
0f265a3
inheritance checks
krakjoe Mar 7, 2016
027f0a1
a todo
krakjoe Mar 7, 2016
df9556b
nope
krakjoe Mar 7, 2016
64bce1e
some more resolution stuff
krakjoe Mar 7, 2016
3f56cd9
more inheritance stuff
krakjoe Mar 7, 2016
95eed91
fix ast pretty printer
krakjoe Mar 7, 2016
ab3e992
refactor a little
krakjoe Mar 7, 2016
48c98ab
add unset test
krakjoe Mar 7, 2016
0b4fb46
abominable reading ahead of the unknown ... in the dark, scarily ....…
krakjoe Mar 7, 2016
4d496ef
some mo tests
krakjoe Mar 7, 2016
95a9e0d
fixes and more stuff
krakjoe Mar 7, 2016
49ea1df
woops
krakjoe Mar 7, 2016
b557f8b
don't check private property types during inheritance
krakjoe Mar 7, 2016
c957e75
check type on declaration
krakjoe Mar 7, 2016
c156399
more tests, check initial value types
krakjoe Mar 7, 2016
861287a
remove todo
krakjoe Mar 7, 2016
f7f8c1b
@TazeTSchnitzel rescued me from hell ...
krakjoe Mar 7, 2016
ce47372
better error messages
krakjoe Mar 8, 2016
7159da6
cache class entry in property info
krakjoe Mar 8, 2016
ef2531c
nothing
krakjoe Mar 8, 2016
414fb83
resolve property type
krakjoe Mar 8, 2016
8095119
add void check during compile
krakjoe Mar 8, 2016
f5fdbf1
tidy
krakjoe Mar 8, 2016
8679c7a
allow overflow of int->long, deref container in fetch_obj_w
krakjoe Mar 8, 2016
40b1409
functions are good ?
krakjoe Mar 8, 2016
6077171
more refactor
krakjoe Mar 8, 2016
6e961f0
urm ... we'll want a return value that makes sense
krakjoe Mar 8, 2016
a98b4b3
remove todo
krakjoe Mar 8, 2016
0825a9d
...
krakjoe Mar 8, 2016
abdde6e
betterness
krakjoe Mar 8, 2016
7d12158
restrict declaration groups
krakjoe Mar 9, 2016
2c3e242
comments
krakjoe Mar 9, 2016
e5afe08
nulling these seems like a good idea
krakjoe Mar 9, 2016
fb1ef8a
use flags here
krakjoe Mar 9, 2016
b4ec5eb
use flags here too
krakjoe Mar 9, 2016
90e18db
test widening, fix uninitialized access check
krakjoe Mar 9, 2016
17f749e
description corrections
krakjoe Mar 9, 2016
476805f
can never be an object at compile time
krakjoe Mar 9, 2016
c7306b5
more verify in fetch_obj_r
krakjoe Mar 9, 2016
1fa8106
add tests for obj_read_r coerced value
krakjoe Mar 9, 2016
b9f8e85
fix binary assign helper
krakjoe Mar 9, 2016
8dbdbf9
woops
krakjoe Mar 9, 2016
4e8988a
update test desc
krakjoe Mar 9, 2016
3f24be0
delay check for constant/ast during compile
krakjoe Mar 9, 2016
373b2a5
add test for that^
krakjoe Mar 9, 2016
d2ab6d2
moar tests
krakjoe Mar 9, 2016
fa835f3
refactor switch
krakjoe Mar 9, 2016
1d8a9d7
squish branches
krakjoe Mar 9, 2016
fb4f14f
disallow typed static properties, throw core errors for internal clas…
krakjoe Mar 9, 2016
5817fa3
makes no sense
krakjoe Mar 9, 2016
d195bce
add tests for inheritance of private props
krakjoe Mar 9, 2016
2beab02
export type from ast ... note that there are still outstanding bugs i…
krakjoe Mar 10, 2016
109ee15
cleanup tests
krakjoe Mar 10, 2016
86ea6ed
fix func arg fetch handler
krakjoe Mar 10, 2016
9a8fb9e
fix mixing typed and untyped properties, add another test for that
krakjoe Mar 10, 2016
0d5d801
reflection changes
krakjoe Mar 11, 2016
29f80f3
the same changes, but nicer
krakjoe Mar 11, 2016
61da1b9
better if this makes sense ... I think
krakjoe Mar 11, 2016
2fde206
ReflectionProperty::hasType
krakjoe Mar 11, 2016
7ed980e
fix trait inheritance
krakjoe Mar 11, 2016
113dc9d
woops
krakjoe Mar 11, 2016
db9e574
typed properties need to be undef initial value
krakjoe Mar 11, 2016
87f1a37
only allow widening int to float at runtime
krakjoe Mar 16, 2016
4c7404c
with much regret and sadness ... use strict types directive
krakjoe Mar 17, 2016
8da4950
fix binary assign op surpriseness
krakjoe Mar 17, 2016
f3ffc3e
merge
krakjoe Mar 17, 2016
d80750e
stuff
krakjoe Mar 17, 2016
37fe31f
Merge branch 'master' of https://github.com/php/php-src into typed-pr…
krakjoe Mar 17, 2016
3628c34
an woops
krakjoe Mar 17, 2016
72e330a
lift unset restriction, possibly temporarily while we argue about stu…
krakjoe Mar 18, 2016
90fde77
add test
krakjoe Mar 18, 2016
8ce9014
tidy up vm stuff
krakjoe Mar 18, 2016
d2df329
some consistency
krakjoe Mar 18, 2016
d01e02b
this kind of type verification only happens at runtime
krakjoe Mar 19, 2016
e3e46f0
info always available
krakjoe Mar 19, 2016
5037a74
improve error message to distinguish between arg by reference and by …
krakjoe Mar 19, 2016
b2d5f55
not needed
krakjoe Mar 19, 2016
5e57cda
check in rw fetch, not sure about this ?
krakjoe Mar 19, 2016
896a4a6
just make travis run again
krakjoe Mar 20, 2016
449ca5d
Merge branch 'master' of https://github.com/php/php-src into typed-pr…
krakjoe Mar 20, 2016
baa0997
the next opline always exists
krakjoe Mar 20, 2016
8886795
require guard here too
krakjoe Mar 20, 2016
658fc85
delay fetching iterator until after guard
krakjoe Mar 20, 2016
1fd296d
--indentation
krakjoe Mar 20, 2016
eed867a
fix return by ref
krakjoe Mar 20, 2016
fced030
do not allow send ref
krakjoe Mar 21, 2016
1dfbed4
add yield by reference guard
krakjoe Mar 21, 2016
b1543e5
guard add array element
krakjoe Mar 21, 2016
3a057cb
use api for stuff ... doh
krakjoe Mar 21, 2016
bc450bb
inheritance fixes
krakjoe Mar 21, 2016
8a8d97a
woops
krakjoe Mar 22, 2016
be570b0
separate proeprty type verification and error routine, fix display of…
krakjoe Mar 24, 2016
f1e0152
Merge branch 'master' of https://github.com/php/php-src into typed-pr…
krakjoe Mar 26, 2016
46bffb2
Merge branch 'master' of https://github.com/php/php-src into typed-pr…
krakjoe Mar 26, 2016
9fc0b75
Merge branch 'master' of https://github.com/php/php-src into typed-pr…
krakjoe Mar 27, 2016
4f1b2e1
better detection
krakjoe Mar 30, 2016
e48ecd8
caches and things ... Thanks Dmitry :)
krakjoe Mar 30, 2016
efe506a
fix foreach, possibly ...
krakjoe Mar 30, 2016
c6eb6d2
fix foreach
krakjoe Apr 4, 2016
cce2414
change decl groups
krakjoe Apr 4, 2016
8f3ad60
overflow consistency
krakjoe Apr 5, 2016
f13b0a3
moar consistent ... even if strange
krakjoe Apr 5, 2016
dd6d5ad
make var_dump more acceptable
krakjoe Apr 5, 2016
a99e7d1
better
krakjoe Apr 5, 2016
a684a36
Merge branch 'master' of https://github.com/php/php-src into typed-pr…
krakjoe Apr 5, 2016
e7d8aa7
haaaaaands
krakjoe Apr 5, 2016
88ff051
add overflowingness test
krakjoe Apr 5, 2016
7e02f50
make travis run
krakjoe Apr 5, 2016
7edbbae
fix test [ci skip]
krakjoe Apr 5, 2016
26e1afb
fix memory error in std_write_property
krakjoe Apr 6, 2016
a2d6d5b
fix another possible memory error
krakjoe Apr 6, 2016
4674286
better guard here and use _ex to avoid ever reading type info, we kno…
krakjoe Apr 6, 2016
ff56cd6
don't perform check on inline properties on read, only for magic
krakjoe Apr 6, 2016
2181b0e
use more efficient macro when possible
krakjoe Apr 6, 2016
e8bac36
fix test case given by dmitry ...
krakjoe Apr 6, 2016
335ca97
only need null check here
krakjoe Apr 6, 2016
239216a
use more efficient stuff here
krakjoe Apr 6, 2016
6f57c44
this was checked on write too
krakjoe Apr 6, 2016
e62942a
safety for non standard objects, remove pointless check
krakjoe Apr 6, 2016
48d5aec
some more improvements ... haaaands
krakjoe Apr 6, 2016
3f8430e
use api stuff here
krakjoe Apr 7, 2016
f391d86
catch more cases here
krakjoe Apr 7, 2016
304ff49
nothin to see here ...
krakjoe Apr 7, 2016
2a5daa7
improve performance
krakjoe May 23, 2016
9461e37
cold error path
krakjoe May 23, 2016
fcaf14a
optimize all the things, thanks @bwoebi
krakjoe May 23, 2016
ecfb1e3
more perf stuff
krakjoe May 23, 2016
f284596
Merge branch 'master' of https://github.com/php/php-src into typed-pr…
krakjoe May 23, 2016
a3e98d7
dtor correct thing
krakjoe May 23, 2016
19de3fc
dtor tmp
krakjoe May 23, 2016
fce9549
check correct variable
krakjoe May 23, 2016
47003bd
Optimize & fix value reassign
bwoebi May 23, 2016
9282b82
Easy optimization: constant assignment to objects will always succeed…
bwoebi May 23, 2016
403dabf
Optimize property fetching and fix edge cases with __get()
bwoebi May 23, 2016
6a0e3c9
Disallow functions to set references and bad types
bwoebi May 24, 2016
7eeda09
missing colons
krakjoe May 24, 2016
c2712a5
Merge branch 'master' into typed-properties
dstogov May 24, 2016
ce442f9
Fixed white-spaces
dstogov May 24, 2016
25ed1f2
Revert insignificant formatting changes to minimize the patch against…
dstogov May 24, 2016
758076a
Fix passed zval reassignments (opcache.protect_memory=1)
bwoebi May 24, 2016
e5f9ada
Respect also immutable values
bwoebi May 24, 2016
88def3f
Added tests
dstogov May 24, 2016
4b28b0d
Added test (error message should be the same independently of called …
dstogov May 24, 2016
2ce08ca
Fixed Zend/tests/type_declarations/typed_properties_044.phpt (error m…
dstogov May 24, 2016
4705b9f
Fixed memory leaks
dstogov May 24, 2016
62b6bef
Avoid type check for dynamic properties
dstogov May 24, 2016
d27efdb
Removed "safety" code for non standard zend objects.
dstogov May 24, 2016
68bb52e
Fixed memory leaks
dstogov May 24, 2016
a1e3ea3
Fixed test (NULL is no compatible with "int" and should produce a error)
dstogov May 24, 2016
4aae938
Get rid of zend_verify_scalar_property_type(), use common zend_verify…
dstogov May 24, 2016
59b8a85
Optimized zend_verify_property_type()
dstogov May 24, 2016
d46584f
Avoid type check for dynamic properties
dstogov May 24, 2016
ff19dec
Added support for nullable typed properties
dstogov May 24, 2016
7d0654e
Added test
dstogov May 24, 2016
852606c
Fix further invalid source zval overwrites
bwoebi May 24, 2016
142e1da
Merge branch 'master' into typed-properties
dstogov May 25, 2016
03861bd
Reverted incomplete fix
dstogov May 25, 2016
be071cd
Fixed test
dstogov May 25, 2016
c992fcf
A better to fix for source zval separation on type conversion caused …
dstogov May 25, 2016
f4b4051
Changed behavior of uninitialized nullable properties (accoding to co…
dstogov May 25, 2016
e657bf8
Revert useless changes
dstogov May 25, 2016
d46695d
Removed useless check (references are never fetched using FETCH_OBJ_RW)
dstogov May 25, 2016
a53b8ac
Removed duplicate code
dstogov May 25, 2016
d248c27
Added support for nullavle types in AST pretty printer
dstogov May 25, 2016
e1017f9
Revert useless change
dstogov May 25, 2016
291d431
Added test (memory leaks)
dstogov May 25, 2016
40aa6c2
"string" mat be also a valid "callable"
dstogov May 25, 2016
3143597
Removed unused function and macro
dstogov May 25, 2016
4506a70
Optimization
dstogov May 25, 2016
05dd2e4
Removed unused zend_object_fetch_property_type_info() and renamed zen…
dstogov May 25, 2016
023df77
Fixed wrong exception when passing element of typed property by refer…
dstogov May 25, 2016
56e2aaa
Optimisation
dstogov May 25, 2016
d7f313e
Added test (failed with valgrind)
dstogov May 25, 2016
892994d
Added test (failed with valgrind)
dstogov May 25, 2016
b0dcf0c
Fixed memory leak
dstogov May 25, 2016
2019f66
Check type after constant resolution
dstogov May 25, 2016
c304c6b
Support for type checks of named constants in property default values.
dstogov May 25, 2016
b2cebf6
Fixed memory leaks
dstogov May 26, 2016
8bc2078
Faster +=/++/--
dstogov May 26, 2016
26bf822
Merge branch 'master' into typed-properties
dstogov May 26, 2016
865686f
Fixed ZTS compatibility
dstogov May 26, 2016
bf875fb
Fixed compilation warning
dstogov May 26, 2016
fc41cdc
fixed white-spaces
dstogov May 26, 2016
8beea3d
Optimization
dstogov May 26, 2016
13c86d9
Cleanup ext/reflection changes
dstogov May 26, 2016
697b1b1
Moved BG(unserialize_refs) into php_unserialize_data_t
dstogov May 26, 2016
c5e45ff
Keep GCC from incorrect over-optimisation
dstogov May 26, 2016
6bb1fa9
Removed useles code (default value should be already properly set in …
dstogov May 27, 2016
5811e86
Don't mess NULL and UNDEF default value back and force. Set proper va…
dstogov May 27, 2016
037da58
Remove duplicate checks
dstogov May 27, 2016
03e7112
Fixed incorrect argument types
dstogov May 27, 2016
cc19dc5
Remove bad checks for simple property inheritance
bwoebi May 28, 2016
3bbe52d
Merge branch 'master' of https://github.com/php/php-src into typed-pr…
krakjoe Jun 7, 2016
de84f50
changes prompted by review
krakjoe Jun 7, 2016
71cda1b
make consistent with RFC (nullables)
krakjoe Jun 7, 2016
8eec27f
fix inheritance error
krakjoe Jun 7, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Zend/tests/type_declarations/typed_properties_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--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 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;
}
});
?>
--EXPECTF--
object(class@anonymous)#%d (5) {
["int"]=>
int(1)
["float"]=>
float(2.2)
["bool"]=>
bool(true)
["array"]=>
array(1) {
[0]=>
string(4) "four"
}
["std"]=>
object(stdClass)#%d (0) {
}
}

17 changes: 17 additions & 0 deletions Zend/tests/type_declarations/typed_properties_002.phpt
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 TypeError: Typed property class@anonymous::$int must not be accessed before initialization in %s:6
Stack trace:
#0 {main}
thrown in %s on line 6


16 changes: 16 additions & 0 deletions Zend/tests/type_declarations/typed_properties_003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Test typed properties error condition (fetch reference)
--FILE--
<?php
$thing = new class() {
public int $int;
};

$var = &$thing->int;
?>
--EXPECTF--
Fatal error: Uncaught TypeError: Typed property class@anonymous::$int must not be referenced in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d

20 changes: 20 additions & 0 deletions Zend/tests/type_declarations/typed_properties_004.phpt
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 integer, 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


20 changes: 20 additions & 0 deletions Zend/tests/type_declarations/typed_properties_005.phpt
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
15 changes: 15 additions & 0 deletions Zend/tests/type_declarations/typed_properties_006.phpt
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 integer (as in class Foo) in %s on line 8

19 changes: 19 additions & 0 deletions Zend/tests/type_declarations/typed_properties_007.phpt
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


16 changes: 16 additions & 0 deletions Zend/tests/type_declarations/typed_properties_008.phpt
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 integer (as in class Foo) in %s on line 8


23 changes: 23 additions & 0 deletions Zend/tests/type_declarations/typed_properties_009.phpt
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);
?>
--EXPECTF--
string(3) "bar"
int(0)
22 changes: 22 additions & 0 deletions Zend/tests/type_declarations/typed_properties_010.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Test typed properties disallow fetch reference for func arg
--FILE--
<?php
class Foo {
public int $bar = 1;
}

$cb = function(int &$bar) {
var_dump($bar);
};

$foo = new Foo();
$cb($foo->bar);
?>
--EXPECTF--
Fatal error: Uncaught TypeError: Typed property Foo::$bar must not be referenced in %s:11
Stack trace:
#0 {main}
thrown in %s on line 11


19 changes: 19 additions & 0 deletions Zend/tests/type_declarations/typed_properties_011.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Test typed properties disallow fetch reference for init array
--FILE--
<?php
class Foo {
public int $bar = 1;
}

$foo = new Foo();

$array = [&$foo->bar];
?>
--EXPECTF--
Fatal error: Uncaught TypeError: Typed property Foo::$bar must not be referenced in %s:8
Stack trace:
#0 {main}
thrown in %s on line 8


18 changes: 18 additions & 0 deletions Zend/tests/type_declarations/typed_properties_012.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Test typed properties disallow fetch reference for foreach
--FILE--
<?php
class Foo {
public int $bar = 1;
}

$foo = new Foo();
foreach ($foo as &$prop);
?>
--EXPECTF--
Fatal error: Uncaught TypeError: Typed properties exist in Foo: foreach by reference is disallowed in %s:7
Stack trace:
#0 {main}
thrown in %s on line 7


14 changes: 14 additions & 0 deletions Zend/tests/type_declarations/typed_properties_013.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Test typed properties disallow incorrect type initial value (scalar)
--FILE--
<?php
class Foo {
public int $bar = "string";
}
?>
--EXPECTF--
Fatal error: Default value for properties with integer type can only be integer in %s on line 3




13 changes: 13 additions & 0 deletions Zend/tests/type_declarations/typed_properties_014.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
Test typed properties disallow incorrect type initial value (array)
--FILE--
<?php
class Foo {
public array $bar = null;
}
?>
--EXPECTF--
Fatal error: Default value for properties with array type can only be an array in %s on line 3



14 changes: 14 additions & 0 deletions Zend/tests/type_declarations/typed_properties_015.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Test typed properties disallow incorrect type initial value (object)
--FILE--
<?php
class Foo {
public stdClass $bar = null;
}
?>
--EXPECTF--
Fatal error: Default value for properties with class type are disallowed in %s on line 3




17 changes: 17 additions & 0 deletions Zend/tests/type_declarations/typed_properties_016.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Test typed properties initial values
--FILE--
<?php
class Foo {
public int $int = 1;
public float $flt = 2.2;
public array $arr = [];
public bool $bool = false;
}
echo "ok\n";
?>
--EXPECT--
ok



16 changes: 16 additions & 0 deletions Zend/tests/type_declarations/typed_properties_017.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Test typed properties disallow void
--FILE--
<?php
class Foo {
public void $int;
}

$foo = new Foo();
?>
--EXPECTF--
Fatal error: Typed property Foo::$int must not be void in %s on line 3




22 changes: 22 additions & 0 deletions Zend/tests/type_declarations/typed_properties_018.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--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());
?>
--EXPECTF--
string(3) "int"





27 changes: 27 additions & 0 deletions Zend/tests/type_declarations/typed_properties_019.phpt
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 integer, float used in %s:6
Stack trace:
#0 %s(12): Foo->inc()
#1 {main}
thrown in %s on line 6





Loading