<?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; //ERROR
// We *can* access the following:
echo __LINE__ . ': ' . $this->basePrivate . "\n"; //OK
echo __LINE__ . ': ' . $this->baseProtected . "\n"; //OK
echo __LINE__ . ': ' . $this->basePublic . "\n"; //OK
}
}
class Child extends Base
{
private $childPrivate = 'private in child class';
protected $childProtected = 'protected in child class';
public $childPublic = '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; //ERROR
// We can't access $this->doesNotExist because it doesn't exist.
echo $this->doesNotExist; //ERROR
// We *can* access the following:
echo __LINE__ . ': ' . $this->baseProtected . "\n"; //OK
echo __LINE__ . ': ' . $this->basePublic . "\n"; //OK
echo __LINE__ . ': ' . $this->childPrivate . "\n"; //OK
echo __LINE__ . ': ' . $this->childProtected . "\n"; //OK
echo __LINE__ . ': ' . $this->childPublic . "\n"; //OK
}
}
// 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; //ERROR
echo $base->basePrivate; //ERROR
echo $base->baseProtected; //ERROR
echo $child->doesNotExist; //ERROR
echo $child->basePrivate; //ERROR
echo $child->baseProtected; //ERROR
echo $child->childPrivate; //ERROR
echo $child->childProtected; //ERROR
// Outside the class, we *can* access public properties
echo __LINE__ . ': ' . $base->basePublic . "\n"; //OK
echo __LINE__ . ': ' . $child->basePublic . "\n"; //OK
echo __LINE__ . ': ' . $child->childPublic . "\n"; //OK
Warning: Undefined property: Base::$doesNotExist in /in/iYo9i 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/iYo9i on line 31
Warning: Undefined property: Child::$doesNotExist in /in/iYo9i on line 34
37: protected in base class
38: public in base class
39: private in child class
40: protected in child class
41: public in child class
Warning: Undefined property: Base::$doesNotExist in /in/iYo9i on line 56
Fatal error: Uncaught Error: Cannot access private property Base::$basePrivate in /in/iYo9i:57
Stack trace:
#0 {main}
thrown in /in/iYo9i on line 57
Process exited with code 255.
Notice: Undefined property: Base::$doesNotExist in /in/iYo9i 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/iYo9i on line 31
Notice: Undefined property: Child::$doesNotExist in /in/iYo9i on line 34
37: protected in base class
38: public in base class
39: private in child class
40: protected in child class
41: public in child class
Notice: Undefined property: Base::$doesNotExist in /in/iYo9i on line 56
Fatal error: Uncaught Error: Cannot access private property Base::$basePrivate in /in/iYo9i:57
Stack trace:
#0 {main}
thrown in /in/iYo9i 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: public in child class
Fatal error: Uncaught Error: Cannot access private property Base::$basePrivate in /in/iYo9i:57
Stack trace:
#0 {main}
thrown in /in/iYo9i on line 57
Process exited with code 255.
Notice: Undefined property: Base::$doesNotExist in /in/iYo9i 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/iYo9i on line 31
Notice: Undefined property: Child::$doesNotExist in /in/iYo9i on line 34
37: protected in base class
38: public in base class
39: private in child class
40: protected in child class
41: public in child class
Notice: Undefined property: Base::$doesNotExist in /in/iYo9i on line 56
Fatal error: Cannot access private property Base::$basePrivate in /in/iYo9i on line 57
Process exited with code 255.