PHP OOP : Access Modifier (public, protected, private)

What is the access modifier?

Restrictions on properties and methods.

Types of access modifier

  1. public
  2. protected
  3. private

Public :

  • Access on base as well as derived class and outside of the class.
  • Everyone can see and access the data.
  • Not secure.
  • Override use.

Protected :

  • Base and Derived class.
  • No one gets access throughout the class.
  • Declare like protected property and method.
  • Not use override function.
  • Without permission no one can access any of these (properties & method).

Private :

  • Access itself.
  • Highly secured.
  • No one can access without permission.
  • Highly costly.
Tagged : / / / / / / / / / / / / / / /

PHP OOP : Inheritance

What is inheritance in php?

  • Inheritance is properties of PHP.
  • Accessing the feature of one class of another class.
  • Three types that is single, multiple and multilevel inheritance.
  • It is the four pillars of oops.
  • It allows a class to reuse the code from another class without duplicating it.
  • Two classes one is base/main another one is derived class.
  • Base class only use its own function but in Derived class it use both function base or derived class.

How to use inheritance in php?

class school {
}
class students extends school {
}
$sch =new school (); // only use class school 
$stu = new students (); // use both class school students (properties and methods)

Example of main/base class use :-

Example of inheritance:

Tagged : / / / / / / / / / / / / / /