3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Base { private $basePrivate = 'private in base class'; protected $baseProtected = 'protected in base class'; public $basePublic = 'public in base class'; public function baseExample() { // We *can't* access $this->doesNotExist because it doesn't exist. echo $this->doesNotExist; // We *can* access the following: echo __LINE__ . ': ' . $this->basePrivate . "\n"; echo __LINE__ . ': ' . $this->baseProtected . "\n"; echo __LINE__ . ': ' . $this->basePublic . "\n"; } } class Child extends Base { private $childPrivate = 'private in child class'; protected $childProtected = 'protected in child class'; public $childPubilc = 'public in child class'; public function childExample() { // We can't access $this->basePrivate because the base class // declared it as private (this is the what private means) echo $this->basePrivate; // We can't access $this->doesNotExist because it doesn't exist. echo $this->doesNotExist; // We *can* access the following: echo __LINE__ . ': ' . $this->baseProtected . "\n"; echo __LINE__ . ': ' . $this->basePublic . "\n"; echo __LINE__ . ': ' . $this->childPrivate . "\n"; echo __LINE__ . ': ' . $this->childProtected . "\n"; echo __LINE__ . ': ' . $this->childPublic . "\n"; } } // Now we'll create instances of these classes. Since we're now outside of the // classes (i.e. we're using instances of the classes and not in the actual code of the class) // we can now only access public properties. $base = new Base; $child = new Child; // Let's run our demo methods to see the results echo $base->baseExample(); echo $child->childExample(); // Outside the class, we *can't* access the following: echo $base->doesNotExist; echo $base->basePrivate; echo $base->baseProtected; echo $child->doesNotExist; echo $child->basePrivate; echo $child->baseProtected; echo $child->childPrivate; echo $child->childProtected; // Outside the class, we *can* access public properties echo __LINE__ . ': ' . $base->basePublic . "\n"; echo __LINE__ . ': ' . $child->basePublic . "\n"; echo __LINE__ . ': ' . $child->childPublic . "\n";
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
Warning: Undefined property: Base::$doesNotExist in /in/IhXsh on line 12 15: private in base class 16: protected in base class 17: public in base class Warning: Undefined property: Child::$basePrivate in /in/IhXsh on line 31 Warning: Undefined property: Child::$doesNotExist in /in/IhXsh on line 34 37: protected in base class 38: public in base class 39: private in child class 40: protected in child class Warning: Undefined property: Child::$childPublic in /in/IhXsh on line 41 41: Warning: Undefined property: Base::$doesNotExist in /in/IhXsh on line 56 Fatal error: Uncaught Error: Cannot access private property Base::$basePrivate in /in/IhXsh:57 Stack trace: #0 {main} thrown in /in/IhXsh on line 57
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.31, 7.4.0 - 7.4.33
Notice: Undefined property: Base::$doesNotExist in /in/IhXsh on line 12 15: private in base class 16: protected in base class 17: public in base class Notice: Undefined property: Child::$basePrivate in /in/IhXsh on line 31 Notice: Undefined property: Child::$doesNotExist in /in/IhXsh on line 34 37: protected in base class 38: public in base class 39: private in child class 40: protected in child class Notice: Undefined property: Child::$childPublic in /in/IhXsh on line 41 41: Notice: Undefined property: Base::$doesNotExist in /in/IhXsh on line 56 Fatal error: Uncaught Error: Cannot access private property Base::$basePrivate in /in/IhXsh:57 Stack trace: #0 {main} thrown in /in/IhXsh on line 57
Process exited with code 255.
Output for 7.3.32 - 7.3.33
15: private in base class 16: protected in base class 17: public in base class 37: protected in base class 38: public in base class 39: private in child class 40: protected in child class 41: Fatal error: Uncaught Error: Cannot access private property Base::$basePrivate in /in/IhXsh:57 Stack trace: #0 {main} thrown in /in/IhXsh on line 57
Process exited with code 255.
Output for 5.4.2 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.28
Notice: Undefined property: Base::$doesNotExist in /in/IhXsh on line 12 15: private in base class 16: protected in base class 17: public in base class Notice: Undefined property: Child::$basePrivate in /in/IhXsh on line 31 Notice: Undefined property: Child::$doesNotExist in /in/IhXsh on line 34 37: protected in base class 38: public in base class 39: private in child class 40: protected in child class Notice: Undefined property: Child::$childPublic in /in/IhXsh on line 41 41: Notice: Undefined property: Base::$doesNotExist in /in/IhXsh on line 56 Fatal error: Cannot access private property Base::$basePrivate in /in/IhXsh on line 57
Process exited with code 255.

preferences:
169.61 ms | 403 KiB | 229 Q