File tree Expand file tree Collapse file tree 8 files changed +121
-12
lines changed
type_declarations/variance Expand file tree Collapse file tree 8 files changed +121
-12
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,8 @@ class qux implements foo {
14
14
}
15
15
16
16
$ qux = new qux ();
17
- var_dump ($ qux ->bar ());
18
- --EXPECTF --
19
- Fatal error: Declaration of qux::bar(): qux must be compatible with foo::bar (): foo in %s008.php on line 8
17
+ echo get_class ($ qux ->bar ());
18
+
19
+ ?>
20
+ --EXPECT--
21
+ qux
Original file line number Diff line number Diff line change @@ -15,6 +15,8 @@ class SomeCollection implements Collection {
15
15
}
16
16
17
17
$ some = new SomeCollection ();
18
- var_dump ($ some ->getIterator ());
19
- --EXPECTF --
20
- Fatal error: Declaration of SomeCollection::getIterator(): Generator must be compatible with Collection::getIterator (): Iterator in %sgenerators003.php on line 7
18
+ echo get_class ($ some ->getIterator ());
19
+
20
+ ?>
21
+ --EXPECT--
22
+ Generator
Original file line number Diff line number Diff line change @@ -13,5 +13,9 @@ class Bar extends Foo {
13
13
return new Bar ;
14
14
}
15
15
}
16
- --EXPECTF --
17
- Fatal error: Declaration of Bar::test(): Bar must be compatible with Foo::test (): Foo in %sinheritance005.php on line 9
16
+
17
+ echo get_class (Bar::test ());
18
+
19
+ ?>
20
+ --EXPECT--
21
+ Bar
Original file line number Diff line number Diff line change @@ -17,5 +17,9 @@ class Bar extends Foo {
17
17
return new B ;
18
18
}
19
19
}
20
- --EXPECTF --
21
- Fatal error: Declaration of Bar::test(): B must be compatible with Foo::test (): A in %sinheritance006.php on line 11
20
+
21
+ echo get_class (Bar::test ());
22
+
23
+ ?>
24
+ --EXPECT--
25
+ B
Original file line number Diff line number Diff line change @@ -15,5 +15,9 @@ class Bar extends Foo {
15
15
return new ArrayObject ([1 , 2 ]);
16
16
}
17
17
}
18
- --EXPECTF --
19
- Fatal error: Declaration of Bar::test(): ArrayObject must be compatible with Foo::test (): Traversable in %sinheritance007.php on line 9
18
+
19
+ echo get_class (Bar::test ());
20
+
21
+ ?>
22
+ --EXPECT--
23
+ ArrayObject
Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ Forward compatibility with types that look like classes but aren't
3
+ --FILE--
4
+ <?php
5
+
6
+ spl_autoload_register (function ($ class ) {
7
+ var_dump ($ class );
8
+ if ($ class === 'X ' ) {
9
+ class X {}
10
+ } else {
11
+ class Y {}
12
+ }
13
+ });
14
+
15
+ class A {
16
+ public function method (X $ param ) : object {}
17
+ }
18
+
19
+ class B extends A {
20
+ public function method (object $ param ) : Y {}
21
+ }
22
+
23
+ ?>
24
+ --EXPECT--
25
+ string(1) "X"
26
+ string(1) "Y"
Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ Testing object's variance in inheritance
3
+ --FILE--
4
+ <?php
5
+
6
+ interface I1 {
7
+ function method1 (I1 $ o ): object ;
8
+ }
9
+ interface I2 extends I1 {
10
+ function method1 (object $ o ): I1 ;
11
+ }
12
+ final class C1 implements I2 {
13
+ function method1 ($ o = null ): self {
14
+ return $ this ;
15
+ }
16
+ }
17
+
18
+ $ o = new C1 ();
19
+ echo get_class ($ o ->method1 ());
20
+ ?>
21
+ --EXPECT--
22
+ C1
Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ Use of parent inside a class that has / has no parent
3
+ --FILE--
4
+ <?php
5
+
6
+ // Illegal: A::parent is ill-defined
7
+ class A {
8
+ public function method (parent $ x ) {}
9
+ }
10
+ class B extends A {
11
+ public function method (parent $ x ) {}
12
+ }
13
+
14
+ // Legal: A2::parent == P2
15
+ class P2 {}
16
+ class A2 extends P2 {
17
+ public function method (parent $ x ) {}
18
+ }
19
+ class B2 extends A2 {
20
+ public function method (P2 $ x ) {}
21
+ }
22
+
23
+ // Legal: B3::parent == A3 is subclass of A3::parent == P3 in covariant position
24
+ class P3 {}
25
+ class A3 extends P3 {
26
+ public function method ($ x ): parent {}
27
+ }
28
+ class B3 extends A3 {
29
+ public function method ($ x ): parent {}
30
+ }
31
+
32
+ // Illegal: B4::parent == A4 is subclass of A4::parent == P4 in contravariant position
33
+ class P4 {}
34
+ class A4 extends P4 {
35
+ public function method (parent $ x ) {}
36
+ }
37
+ class B4 extends A4 {
38
+ public function method (parent $ x ) {}
39
+ }
40
+
41
+ ?>
42
+ --EXPECTF--
43
+ Warning: Declaration of B4::method(A4 $x) should be compatible with A4::method(P4 $x) in %s on line %d
44
+
45
+ Warning: Declaration of B::method(A $x) should be compatible with A::method(parent $x) in %s on line %d
You can’t perform that action at this time.
0 commit comments