File tree 10 files changed +301
-0
lines changed
tests/classes/inner_classes
10 files changed +301
-0
lines changed Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ outer class visibility
3
+ --FILE--
4
+ <?php
5
+
6
+ class Outer {
7
+ private class Inner {}
8
+ public Inner $ illegal ;
9
+
10
+ public function test (): void {
11
+ $ this ->illegal = new Inner ();
12
+ }
13
+ }
14
+
15
+ $ x = new Outer ();
16
+ $ x ->test ();
17
+
18
+ var_dump ($ x );
19
+ ?>
20
+ --EXPECTF--
21
+ Fatal error: Uncaught TypeError: Cannot assign private Outer\Inner to higher visibile property Outer::illegal in %s:%d
22
+ Stack trace:
23
+ #0 %s(%d): Outer->test()
24
+ #1 {main}
25
+ thrown in %s on line %d
Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ accessing outer class private vars
3
+ --FILE--
4
+ <?php
5
+
6
+ class Outer {
7
+ private class Inner {
8
+ public function test (Outer $ i ) {
9
+ $ i ->illegal = $ this ;
10
+ }
11
+ }
12
+ private Inner $ illegal ;
13
+
14
+ public function test (): void {
15
+ new Inner ()->test ($ this );
16
+ }
17
+ }
18
+
19
+ $ x = new Outer ();
20
+ $ x ->test ();
21
+
22
+ var_dump ($ x );
23
+
24
+ ?>
25
+ --EXPECT--
26
+ object(Outer)#1 (1) {
27
+ ["illegal":"Outer":private]=>
28
+ object(Outer\Inner)#2 (0) {
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ accessing outer protected vars
3
+ --FILE--
4
+ <?php
5
+
6
+ class Outer {
7
+ private class Inner {
8
+ public function test (Outer $ i ) {
9
+ $ i ->illegal = $ this ;
10
+ }
11
+ }
12
+ private Inner $ illegal ;
13
+
14
+ public function test (): void {
15
+ new Inner ()->test ($ this );
16
+ }
17
+ }
18
+
19
+ $ x = new Outer ();
20
+ $ x ->test ();
21
+
22
+ var_dump ($ x );
23
+
24
+ ?>
25
+ --EXPECT--
26
+ object(Outer)#1 (1) {
27
+ ["illegal":"Outer":private]=>
28
+ object(Outer\Inner)#2 (0) {
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ outer class visibility
3
+ --FILE--
4
+ <?php
5
+
6
+ class Outer {
7
+ protected class Inner {}
8
+ public Inner $ illegal ;
9
+
10
+ public function test (): void {
11
+ $ this ->illegal = new Inner ();
12
+ }
13
+ }
14
+
15
+ $ x = new Outer ();
16
+ $ x ->test ();
17
+
18
+ var_dump ($ x );
19
+ ?>
20
+ --EXPECTF--
21
+ Fatal error: Uncaught TypeError: Cannot assign protected Outer\Inner to higher visibile property Outer::illegal in %s:%d
22
+ Stack trace:
23
+ #0 %s(%d): Outer->test()
24
+ #1 {main}
25
+ thrown in %s on line %d
Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ accessing outer private methods
3
+ --FILE--
4
+ <?php
5
+
6
+ class Outer {
7
+ private function test () {
8
+ echo __METHOD__ . "\n" ;
9
+ }
10
+ class Middle {
11
+ private static function test () {
12
+ echo __METHOD__ . "\n" ;
13
+ }
14
+ class Inner {
15
+ public function test () {
16
+ Middle::test ();
17
+ $ t = new Outer ();
18
+ $ t ->test ();
19
+ }
20
+ }
21
+ }
22
+ }
23
+ new Outer \Middle \Inner ()->test ();
24
+ ?>
25
+ --EXPECT--
26
+ Outer\Middle::test
27
+ Outer::test
Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ scope doesn't bypass scope
3
+ --FILE--
4
+ <?php
5
+
6
+ class Outer {
7
+ private function test () {
8
+ echo __METHOD__ . "\n" ;
9
+ }
10
+ class Middle {
11
+ private static function test () {
12
+ echo __METHOD__ . "\n" ;
13
+ }
14
+ class Inner {
15
+ public function testit () {
16
+ $ this ->test ();
17
+ }
18
+ }
19
+ }
20
+ }
21
+ new Outer \Middle \Inner ()->testit ();
22
+ ?>
23
+ --EXPECTF--
24
+ Fatal error: Uncaught Error: Call to undefined method Outer\Middle\Inner::test() in %s:%d
25
+ Stack trace:
26
+ #0 %s(%d): Outer\Middle\Inner->testit()
27
+ #1 {main}
28
+ thrown in %s on line %d
Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ accessing outer protected methods
3
+ --FILE--
4
+ <?php
5
+
6
+ class Outer {
7
+ protected function test () {
8
+ echo __METHOD__ . "\n" ;
9
+ }
10
+ class Middle {
11
+ protected static function test () {
12
+ echo __METHOD__ . "\n" ;
13
+ }
14
+ class Inner {
15
+ public function test () {
16
+ Middle::test ();
17
+ $ t = new Outer ();
18
+ $ t ->test ();
19
+ }
20
+ }
21
+ }
22
+ }
23
+ new Outer \Middle \Inner ()->test ();
24
+ ?>
25
+ --EXPECT--
26
+ Outer\Middle::test
27
+ Outer::test
Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ accessing sibling methods
3
+ --FILE--
4
+ <?php
5
+
6
+ class Outer {
7
+ protected function test () {
8
+ echo __METHOD__ . "\n" ;
9
+ }
10
+ protected class Middle {
11
+ public static function test () {
12
+ echo __METHOD__ . "\n" ;
13
+ }
14
+ }
15
+ }
16
+
17
+ class Other extends Outer {
18
+ class Inner {
19
+ public function test () {
20
+ Outer \Middle::test ();
21
+ $ t = new Outer ();
22
+ $ t ->test ();
23
+ }
24
+ }
25
+ }
26
+ new Other \Inner ()->test ();
27
+ ?>
28
+ --EXPECT--
29
+ Outer\Middle::test
30
+ Outer::test
Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ deeply nested property visibility
3
+ --FILE--
4
+ <?php
5
+
6
+ class Outer {
7
+ private int $ i ;
8
+ private static int $ j ;
9
+ class Middle {
10
+ private int $ i ;
11
+ private static int $ j ;
12
+ class Inner {
13
+ public static function test () {
14
+ var_dump (Outer::$ j = 5 );
15
+ var_dump (Middle::$ j = 42 );
16
+ $ foo = new Outer ();
17
+ $ foo ->i = 42 ;
18
+ var_dump ($ foo );
19
+ $ foo = new Middle ();
20
+ $ foo ->i = 42 ;
21
+ var_dump ($ foo );
22
+ }
23
+ }
24
+ }
25
+ }
26
+ Outer \Middle \Inner::test ();
27
+ ?>
28
+ --EXPECT--
29
+ int(5)
30
+ int(42)
31
+ object(Outer)#1 (1) {
32
+ ["i":"Outer":private]=>
33
+ int(42)
34
+ }
35
+ object(Outer\Middle)#2 (1) {
36
+ ["i":"Outer\Middle":private]=>
37
+ int(42)
38
+ }
Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ constructors
3
+ --FILE--
4
+ <?php
5
+
6
+ class User {
7
+ public private(set) string $ name ;
8
+ public private(set) string $ email ;
9
+
10
+ private function __construct (Builder $ builder ) {
11
+ $ this ->name = $ builder ->name ;
12
+ $ this ->email = $ builder ->email ;
13
+ }
14
+
15
+ public readonly final class Builder {
16
+ public function __construct (public private(set ) string |null $ name = null , public private(set ) string |null $ email = null ) {}
17
+
18
+ public function withEmail (string $ email ): self {
19
+ return new self ($ this ->name , $ email );
20
+ }
21
+
22
+ public function withName (string $ name ): self {
23
+ return new self ($ name , $ this ->email );
24
+ }
25
+
26
+ public function build (): User {
27
+ return new User ($ this );
28
+ }
29
+ }
30
+ }
31
+
32
+ $ user =
new User \
Builder ()->
withName (
'Rob ' )->
withEmail (
'[email protected] ' )->
build ();
33
+ var_dump ($ user );
34
+ ?>
35
+ --EXPECT--
36
+ object(User)#2 (2) {
37
+ ["name"]=>
38
+ string(3) "Rob"
39
+ ["email"]=>
40
+
41
+ }
You can’t perform that action at this time.
0 commit comments