PHP protected Keyword

PHP public Keyword
PHP private Keyword

In this article, you will learn about protected keyword in PHP. The protected keyword in PHP is an access modifier. It marks a property or method as protected.

Protected methods and properties are accessible within the class and the class derived from it.

examples of the PROTECTED function

Example 1. In this example, we use protected to prevent outside code from modifying a property,

<?php
class MyClass {
  protected $number = 0;
}

class AnotherClass {
  public function add1() {
    $this->number++;
  }

  public function getNumber() {
    return $this->number;
  }
}

$obj = new AnotherClass();
$obj->add1();
echo "The number is " . $obj->getNumber();
?>
PHP public Keyword
PHP private Keyword

Stay up-to-date about PHP!

We don’t spam!

en English
X
Scroll to Top