Php Oop Access Modifiers

In Php, we use access modifiers with method or function or variables. Access modifiers control where method and properties will be accessible.

Access modifiers are 3 types.

  • public:
    This is by default,the property or method can access from everywhere.
  • protected:
    The property or method can access within class itself and by derived class.
  • private:
    The property or method can access within class.

class Student {
  public $name;
  public $class;
  public $course;

 function set_name($n) { // a public function (default)
  $this->name = $n;

 protected function set_class($n) { // a protected function
   $this->class = $n;

 private function set_course($n) { // a private function
  $this->course = $n;


$student = new Student();
$student->set_name('Ashish'); // OK
$student->set_class('10th'); // ERROR
$student->set_course('maths'); // ERROR

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow by Email