3v4l.org

run code in 300+ PHP versions simultaneously
<?php class WP_REST_Global_Styles_Controller { public function update_item() { echo "\n" . __METHOD__ . "\n"; $this->prepare_item_for_database(); } protected function prepare_item_for_database() { echo "\n" . __METHOD__ . "\n"; $this->validate_custom_css(); } private function validate_custom_css() { echo __METHOD__ . "()\n"; } } class Gutenberg_REST_Global_Styles_Controller extends WP_REST_Global_Styles_Controller { protected function prepare_item_for_database() { echo "\n" . __METHOD__ . "\n"; $this->validate_custom_css(); } /** * Copy of the parent's method. * The parent's method is private, meaning a child cannot overload or invoke it. * But a child class can have a method with the same name and different visibility. */ protected function validate_custom_css() { echo __METHOD__ . "()\n"; } } class Gutenberg_REST_Global_Styles_Controller_X_X extends Gutenberg_REST_Global_Styles_Controller {} class WP_REST_Global_Styles_Controller_X_X extends WP_REST_Global_Styles_Controller { // will cause a fatal error. protected function prepare_item_for_database() { // overloads the parent's method. echo "\n" . __METHOD__ . "\n"; $this->validate_custom_css(); // will cause a fatal error, as the parent's method is private to it. } } ( new Gutenberg_REST_Global_Styles_Controller() )->update_item(); ( new Gutenberg_REST_Global_Styles_Controller_X_X() )->update_item(); ( new WP_REST_Global_Styles_Controller_X_X() )->update_item();
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.26, 8.3.0 - 8.3.14, 8.4.1
WP_REST_Global_Styles_Controller::update_item Gutenberg_REST_Global_Styles_Controller::prepare_item_for_database Gutenberg_REST_Global_Styles_Controller::validate_custom_css() WP_REST_Global_Styles_Controller::update_item Gutenberg_REST_Global_Styles_Controller::prepare_item_for_database Gutenberg_REST_Global_Styles_Controller::validate_custom_css() WP_REST_Global_Styles_Controller::update_item WP_REST_Global_Styles_Controller_X_X::prepare_item_for_database Fatal error: Uncaught Error: Call to private method WP_REST_Global_Styles_Controller::validate_custom_css() from scope WP_REST_Global_Styles_Controller_X_X in /in/J5u29:41 Stack trace: #0 /in/J5u29(6): WP_REST_Global_Styles_Controller_X_X->prepare_item_for_database() #1 /in/J5u29(47): WP_REST_Global_Styles_Controller->update_item() #2 {main} thrown in /in/J5u29 on line 41
Process exited with code 255.

preferences:
51.19 ms | 408 KiB | 5 Q