Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RFC: https://wiki.php.net/rfc/typed_properties_v2 This is a squash of PR #3734, which is a squash of PR #3313. Co-authored-by: Bob Weinand <bobwei9@hotmail.com> Co-authored-by: Joe Watkins <krakjoe@php.net> Co-authored-by: Dmitry Stogov <dmitry@zend.com>
- Loading branch information
1 parent
fe8fdfa
commit e219ec1
Showing
210 changed files
with
23,045 additions
and
5,961 deletions.
There are no files selected for viewing
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
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
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,44 @@ | ||
--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 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 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 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 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 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 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 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 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,14 @@ | ||
--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 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 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 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,14 @@ | ||
--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 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 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 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 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 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 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 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 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 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 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 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 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,22 @@ | ||
--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(); | ||
|
||
try { | ||
$foo->inc(); | ||
} catch (TypeError $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
?> | ||
--EXPECT-- | ||
Cannot increment property Foo::$bar of type int past its maximal value |
Oops, something went wrong.