WikiGalaxy

Personalize

PHP Access Modifiers: Introduction

What are Access Modifiers?

Access modifiers in PHP are keywords used to specify the visibility of a property or method. They define how and from where these properties and methods can be accessed.

Public Access Modifier

Public Modifier

Properties and methods declared as public can be accessed from anywhere, both inside and outside of the class.


class MyClass {
  public $name;
  
  public function showName() {
    return $this->name;
  }
}

$obj = new MyClass();
$obj->name = "John";
echo $obj->showName(); // Output: John
    

Private Access Modifier

Private Modifier

Private properties and methods can only be accessed within the class that defines them. They are not accessible from outside the class.


class MyClass {
  private $name;
  
  public function setName($name) {
    $this->name = $name;
  }
  
  public function getName() {
    return $this->name;
  }
}

$obj = new MyClass();
$obj->setName("Jane");
echo $obj->getName(); // Output: Jane
    

Protected Access Modifier

Protected Modifier

Protected properties and methods can be accessed within the class itself and by inheriting classes. They are not accessible from outside.


class ParentClass {
  protected $name;
  
  protected function setName($name) {
    $this->name = $name;
  }
}

class ChildClass extends ParentClass {
  public function setNameAndShow($name) {
    $this->setName($name);
    return $this->name;
  }
}

$obj = new ChildClass();
echo $obj->setNameAndShow("Alice"); // Output: Alice
    

Default Access Modifier

Default Behavior

In PHP, if no access modifier is specified, the property or method is treated as public by default.


class MyClass {
  var $name; // Default is public
  
  function showName() {
    return $this->name;
  }
}

$obj = new MyClass();
$obj->name = "Bob";
echo $obj->showName(); // Output: Bob
    

Using Access Modifiers with Inheritance

Inheritance with Modifiers

When a class inherits from another, it can access the protected and public members of the parent class. Private members remain inaccessible.


class BaseClass {
  protected $name;
  
  public function setName($name) {
    $this->name = $name;
  }
}

class DerivedClass extends BaseClass {
  public function getName() {
    return $this->name;
  }
}

$obj = new DerivedClass();
$obj->setName("Charlie");
echo $obj->getName(); // Output: Charlie
    

Access Modifiers in Interfaces

Modifiers in Interfaces

In PHP, all methods declared in an interface must be public. Interfaces do not allow private or protected methods.


interface MyInterface {
  public function myMethod();
}

class MyClass implements MyInterface {
  public function myMethod() {
    return "Implemented Method";
  }
}

$obj = new MyClass();
echo $obj->myMethod(); // Output: Implemented Method
    

Static Methods and Access Modifiers

Static Methods

Static methods can have any access modifier. They belong to the class rather than any object instance.


class MyClass {
  public static function myStaticMethod() {
    return "Static Method";
  }
}

echo MyClass::myStaticMethod(); // Output: Static Method
    

Final Methods and Access Modifiers

Final Methods

A method declared as final cannot be overridden by child classes. It can have any access modifier.


class MyClass {
  final public function myFinalMethod() {
    return "Final Method";
  }
}

class ChildClass extends MyClass {
  // Cannot override myFinalMethod
}

$obj = new MyClass();
echo $obj->myFinalMethod(); // Output: Final Method
    

Abstract Methods and Access Modifiers

Abstract Methods

Abstract methods must be declared with a visibility keyword. They are intended to be implemented in derived classes.


abstract class MyClass {
  abstract protected function myAbstractMethod();
}

class ConcreteClass extends MyClass {
  protected function myAbstractMethod() {
    return "Implemented Abstract Method";
  }
}

$obj = new ConcreteClass();
echo $obj->myAbstractMethod(); // Output: Implemented Abstract Method
    
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025