In the PHP each and every property of a class in must have one of three visibility levels, known as public, private, and protected.
public
– the property or method can be accessed from everywhere. This is defaultprotected
– the property or method can be accessed within the class and by classes derived from that classprivate
– the property or method can ONLY be accessed within the class
<?php class Cars { public $name; protected $color; private $weight; } $mango = new Cars(); $mango->name = 'Maruti'; // OK $mango->color = 'Yellow'; // ERROR $mango->weight = '300'; // ERROR ?>
<?php class Fruit { public $name; public $color; public $weight; function set_name($n) { // a public function (default) $this->name = $n; } protected function set_color($n) { // a protected function $this->color = $n; } private function set_weight($n) { // a private function $this->weight = $n; } } $mango = new Fruit(); $mango->set_name('Mango'); // OK $mango->set_color('Yellow'); // ERROR $mango->set_weight('300'); // ERROR ?>