Skip to content

Commit 8e10c9d

Browse files
brzuchalnikic
authored andcommitted
Implement object type annotation
RFC: https://wiki.php.net/rfc/object-typehint
1 parent dd15b34 commit 8e10c9d

24 files changed

+363
-116
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ PHP NEWS
88
. Fixed bug #74761 (Unary operator expected error on some systems). (petk)
99
. Allow loading PHP/Zend extensions by name in ini files (extension=<name>).
1010
(francois at tekwire dot net)
11+
. Added object type annotation. (brzuchal)
1112

1213
- OpenSSL:
1314
. Fixed bug #74798 (pkcs7_en/decrypt does not work if \x0a is used in content).

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ PHP 7.2 UPGRADE NOTES
4141
. Minimum supported Windows versions are Windows 7/Server 2008 R2.
4242
. Initial trait property value compatibility check will no longer perform
4343
any casts. (Bug #74269)
44+
. "object" (in any case) can no longer be used as a class name.
4445

4546
- BCMath:
4647
. The bcmod() function no longer truncates fractional numbers to integers. As
@@ -101,6 +102,8 @@ PHP 7.2 UPGRADE NOTES
101102
(https://wiki.php.net/rfc/allow-abstract-function-override)
102103
. A trailing comma in group use statements is now allowed.
103104
(https://wiki.php.net/rfc/list-syntax-trailing-commas)
105+
. The "object" type annotation is now supported.
106+
(https://wiki.php.net/rfc/object-typehint)
104107

105108
- DBA:
106109
. Implemented support for the LMDB backend.

Zend/tests/bug26698.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Bug #26698 (Thrown exceptions while evaluting argument to pass as parameter cras
55

66
ini_set("report_memleaks", 0); // the exception thrown in this test results in a memory leak, which is fine
77

8-
class Object
8+
class ObjectOne
99
{
1010
function getNone()
1111
{
@@ -23,7 +23,7 @@ class Proxy
2323
{
2424
try
2525
{
26-
$res = new Object();
26+
$res = new ObjectOne();
2727
$this->three($res->getNone());
2828
}
2929
catch(Exception $e)
@@ -36,7 +36,7 @@ class Proxy
3636
{
3737
try
3838
{
39-
$res = new Object();
39+
$res = new ObjectOne();
4040
$this->three(1, $res->getNone());
4141
}
4242
catch(Exception $e)
@@ -49,7 +49,7 @@ class Proxy
4949
{
5050
try
5151
{
52-
$res = new Object();
52+
$res = new ObjectOne();
5353
$this->three(1, 2, $res->getNone());
5454
}
5555
catch(Exception $e)

Zend/tests/bug28444.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function my_error_handler($errno, $errstr, $errfile, $errline) {
99

1010
set_error_handler('my_error_handler');
1111

12-
class Object
12+
class ObjectOne
1313
{
1414
public $x;
1515

@@ -26,7 +26,7 @@ class Overloaded
2626

2727
function __construct($x)
2828
{
29-
$this->x = new Object($x);
29+
$this->x = new ObjectOne($x);
3030
}
3131

3232
function __get($prop)
@@ -47,7 +47,7 @@ var_dump($y->x->x);
4747
var_dump($y->x->x = 3);
4848
var_dump($y->y = 3);
4949
var_dump($y->y);
50-
var_dump($y->z = new Object(4));
50+
var_dump($y->z = new ObjectOne(4));
5151
var_dump($y->z->x);
5252
$t = $y->z;
5353
var_dump($t->x = 5);
@@ -56,7 +56,7 @@ var_dump($y->z->x = 6);
5656
?>
5757
===DONE===
5858
--EXPECTF--
59-
object(Object)#%d (1) {
59+
object(ObjectOne)#%d (1) {
6060
["x"]=>
6161
int(2)
6262
}
@@ -66,9 +66,9 @@ Overloaded::__set(y,3)
6666
int(3)
6767
Overloaded::__get(y)
6868
int(3)
69-
string(55) "Object of class Object could not be converted to string"
69+
string(58) "Object of class ObjectOne could not be converted to string"
7070
Overloaded::__set(z,)
71-
object(Object)#%d (1) {
71+
object(ObjectOne)#%d (1) {
7272
["x"]=>
7373
int(4)
7474
}

Zend/tests/bug60598.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ define('OBJECT_COUNT', 10000);
66

77
$containers = array();
88

9-
class Object {
9+
class ObjectOne {
1010
protected $_guid = 0;
1111
public function __construct() {
1212
global $containers;
@@ -20,7 +20,7 @@ class Object {
2020
}
2121

2222
for ($i = 0; $i < OBJECT_COUNT; ++$i) {
23-
new Object();
23+
new ObjectOne();
2424
}
2525

2626
// You probably won't see this because of the "zend_mm_heap corrupted"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Object type can only default to null
3+
--FILE--
4+
<?php
5+
6+
function test(object $obj = 42) { }
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: Default value for parameters with a object type can only be NULL in %s on line %d
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Missing class method a object return type during inheritance
3+
--FILE--
4+
<?php
5+
6+
class One {
7+
public function a() : object {}
8+
}
9+
10+
class Two extends One {
11+
public function a() {}
12+
}
13+
14+
--EXPECTF--
15+
Fatal error: Declaration of Two::a() must be compatible with One::a(): object in %s on line 9
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Missing interface method a object return type during inheritance
3+
--FILE--
4+
<?php
5+
6+
interface One {
7+
public function a() : object;
8+
}
9+
10+
interface Two extends One {
11+
public function a();
12+
}
13+
14+
--EXPECTF--
15+
Fatal error: Declaration of Two::a() must be compatible with One::a(): object in %s on line %d
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Adding a class method object return type
3+
--FILE--
4+
<?php
5+
6+
interface One {
7+
public function a() : object;
8+
}
9+
10+
class Two implements One {
11+
public function a() : object {}
12+
}
13+
14+
$three = new class extends Two {
15+
public function a() : object {
16+
return 12345;
17+
}
18+
};
19+
$three->a();
20+
--EXPECTF--
21+
22+
Fatal error: Uncaught TypeError: Return value of class@anonymous::a() must be an object, integer returned in %s:13
23+
Stack trace:
24+
#0 %s(16): class@anonymous->a()
25+
#1 {main}
26+
thrown in %s on line 13
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Adding a function object return type
3+
--FILE--
4+
<?php
5+
6+
function a() : object {
7+
return 12345;
8+
}
9+
a();
10+
--EXPECTF--
11+
12+
Fatal error: Uncaught TypeError: Return value of a() must be an object, integer returned in %s:4
13+
Stack trace:
14+
#0 %s(6): a()
15+
#1 {main}
16+
thrown in %s on line 4

0 commit comments

Comments
 (0)