How to use the protected access modifier

How the access modifiers work public protected private Access outside class? Access from subclass? Yes No No Yes Yes No

Modifier Access outside class? Access from subclass?
public Yes Yes
protected No Yes
private No No

A superclass

class Person { 
  protected $firstName, $lastName; private $phone, $email; 
       
  // The constructor and the get and set methods are the same // as the Person class
}

A subclass

class Employee extends Person { 
  private $ssn, $hireDate; 
        
  // The constructor and the get and set methods are the same as the Employee class

  // This method uses the protected properties from the Person class 
  public function getFullName() { 
    return $this->lastName . ', ' . $this->firstName;
  }
}

Description

Back