3v4l.org

run code in 300+ PHP versions simultaneously
<?php // User with a protected method class User { protected function getSomething(): string { return 'data from User'; } } // class Student that extend it class Student extends User { public function thisWorks(): void { echo 'Calling protected method from parent: ' . $this->getSomething() . "\n"; } } $s = new Student(); // Note that in this example we call a public method from "the outside" (here). $s->thisWorks(); // This does not work, because this is "global scope". // Protected methods can only be called from withing the class, as in the example above. $s->getSomething();
Output for 8.1.29 - 8.1.33, 8.2.20 - 8.2.29, 8.3.5 - 8.3.25, 8.4.1 - 8.4.12
Calling protected method from parent: data from User Fatal error: Uncaught Error: Call to protected method User::getSomething() from global scope in /in/093Ao:28 Stack trace: #0 {main} thrown in /in/093Ao on line 28
Process exited with code 255.
Output for 7.2.6 - 7.2.34
Calling protected method from parent: data from User Fatal error: Uncaught Error: Call to protected method User::getSomething() from context '' in /in/093Ao:28 Stack trace: #0 {main} thrown in /in/093Ao on line 28
Process exited with code 255.
Output for 5.6.40
Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/093Ao on line 6
Process exited with code 255.

preferences:
62.57 ms | 408 KiB | 5 Q