xref: /PHP-7.2/Zend/tests/bug37632.phpt (revision e97d5fab)
1--TEST--
2Bug #37632 (Protected method access problem)
3--FILE--
4<?php
5
6class A1
7{
8	protected function test()
9	{
10		echo __METHOD__ . "\n";
11	}
12}
13
14class B1 extends A1
15{
16	public function doTest(A1 $obj)
17	{
18		echo __METHOD__ . "\n";
19		$obj->test();
20	}
21}
22
23class C1 extends A1
24{
25	protected function test()
26	{
27		echo __METHOD__ . "\n";
28	}
29}
30
31$b = new B1;
32$b->doTest(new C1);
33
34class A2
35{
36	static protected function test()
37	{
38		echo __METHOD__ . "\n";
39	}
40}
41
42class B2 extends A2
43{
44	static public function doTest(A2 $obj)
45	{
46		echo __METHOD__ . "\n";
47		$obj->test();
48	}
49}
50
51class C2 extends A2
52{
53	static protected function test()
54	{
55		echo __METHOD__ . "\n";
56	}
57}
58
59B2::doTest(new C2);
60
61/* Right now Ctor's cannot be made protected when defined in a ctor. That is
62 * we cannot decrease visibility.
63 *
64
65interface Ctor
66{
67	function __construct($x);
68}
69
70class A3 implements Ctor
71{
72	protected function __construct()
73	{
74		echo __METHOD__ . "\n";
75	}
76}
77
78class B3 extends A3
79{
80	static public function doTest()
81	{
82		echo __METHOD__ . "\n";
83		new C3;
84	}
85}
86
87class C3 extends A3
88{
89	protected function __construct()
90	{
91		echo __METHOD__ . "\n";
92	}
93}
94
95B3::doTest();
96
97*/
98
99class A4
100{
101	protected function __construct()
102	{
103		echo __METHOD__ . "\n";
104	}
105}
106
107class B4 extends A4
108{
109	static public function doTest()
110	{
111		echo __METHOD__ . "\n";
112		new C4;
113	}
114}
115
116class C4 extends A4
117{
118	protected function __construct()
119	{
120		echo __METHOD__ . "\n";
121	}
122}
123
124B4::doTest();
125
126?>
127===DONE===
128--EXPECTF--
129B1::doTest
130C1::test
131B2::doTest
132C2::test
133B4::doTest
134
135Fatal error: Uncaught Error: Call to protected C4::__construct() from context 'B4' in %sbug37632.php:%d
136Stack trace:
137#0 %s(%d): B4::doTest()
138#1 {main}
139  thrown in %sbug37632.php on line %d
140