Public, Protected, and Private -- Not working
| From: | Michael Cinicola | Date: | Wed, 05 Oct 2016 14:40:07 +0000 |
| Subject: | Public, Protected, and Private -- Not working | ||
| Groups: | php.webmaster | ||
| Request: | Send a blank email to php-webmaster+get-25224@lists.php.net to get a copy of this message | ||
The last line in the example:
class MyClass
{
public $public = 'Public';
protected $protected = 'Protected';
private $private = 'Private';
function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
$obj = new MyClass();
echo $obj->public; // Works
echo $obj->protected; // Fatal Error
echo $obj->private; // Fatal Error
$obj->printHello(); // Shows Public, Protected and Private
does not show public, protected, and private:
Tried on Chrome and Safari on macOS Sierra — no result on either.
Source: http://php.net/manual/en/language.oop5.visibility.php
<http://php.net/manual/en/language.oop5.visibility.php>