Access Specifiers

The access specifiers (aka access modifiers) define the level of visibility of a class’s members.


The following scope keywords are used to define the level of access:

  • public
    • members can be accessed any place the object is visible, in the script
  • protected
    • members can only be accessed from within the class itself and inherited/inheriting classes
  • private
    • members are accessible only from other members of the same class
    • explicitly; they are not accessible by inherited/inheriting classes

The $this-> pseudo variable must be used with the property name to access specific properties! The use of the member name on it's own will be ignored.


Example showing public access:

Save & refresh browser:

Hi, my name is Julia


To benefit from the essential OO concept of encapsulation (/ data hiding), it is desirable to keep the properties of the class private / protected, and to make the class responsible for setting and getting its own properties via its own methods only, hence disabling unwanted changes in an object's properties by anything other than the object itself.


The following example shows a protected property that can be inherited by a child class, if this property was set to private it would not be accessible in the child class and could therefore not be used. The example also shows a private property in the child class that can only be accessed within the class:

Save & refresh browser:

Hi, my name is Derrick, and I'm a techie
Julia is a Scientist who works with lazers!!



  • used to to declare that a method or class cannot be overridden by a subclass
  • cannot be applied to properties
  • If the class itself is being defined final then it cannot be extended
  • Means of stopping other programmers using the code in unplanned ways

This example will purposefully fail due to the final keyword being applied to the Person class's speak() function.

Save & refresh browser:

Fatal error: Cannot override final method Person::speak() in /root/user/public_html/php/index.php on line 12


If the child class had not overridden the parent's final declared method, it would have simply used the parent's method with no errors



  • cannot be used directly
  • has to be inherited by a child class

This examples show an abstract class, Person, being inherited by the child, Scientist, class. When the script tries to instantiate the Parent abstract class a fatal error is returned:

Save & refresh browser:

Julia is a Scientist who works with lazers!!
Fatal error: Cannot instantiate abstract class Person in /root/user/public_html/php/index.php on line 17

Leave a Reply