private scope - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of private scope.php

  1. <?php
  2.  
  3. class Foo {
  4.    private function baz() {
  5.         echo 'Foo::baz()' . "\n";
  6.    }
  7.    
  8.    public function callBaz() {
  9.        $this->baz();
  10.    }
  11.  
  12.    public static function callBazWith(Foo $foo) {
  13.        $foo->baz();
  14.    }
  15. }
  16.  
  17. $f = new Foo();
  18.  
  19. try {
  20.         $f->baz();
  21. } catch (Error $e) {
  22.         echo '$f->baz(): ' . $e->getMessage() . "\n";
  23. }
  24.  
  25. try {
  26.         $f->callBaz();
  27. } catch (Error $e) {
  28.         echo '$f->callBaz(): ' . $e->getMessage() . "\n";
  29. }
  30.  
  31. try {
  32.         Foo::callBazWith($f);
  33. } catch (Error $e) {
  34.         echo 'Foo::callBazWith($f): ' . $e->getMessage() . "\n";
  35. }
File Description
  • private scope
  • PHP Code
  • 14 Dec-2018
  • 538 Bytes
You can Share it: