Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks

What is Access Modifiers ?

Aman Jain, July 18, 2021August 2, 2021

In object oriented programming(oops) to set the visibility of classes, variables and methods we are using access modifiers.

So Access modifiers are used the encapsulation of class properties.

There are three types of access modifiers in any language (Php, java or c etc. )

  1. Private
  2. Protected
  3. Public

1. Private Access modifiers
Private access modifiers can only be used in to encapsulate the property of class. we cannot access private property outside the class or in child class as well

<?php 

Class MyClass{
    private $tax=10; //private property
    
    public function calculatePrice($amount){
        return  $this->tax+$amount;                 // we can only use private property within class
    }
    public function getTaxValue(){
        return  $this->tax;                 // private property within class
    }
}

$MyClass    =   new MyClass();
$MyClass->tax;              //will generate error
echo $MyClass->getTaxValue();    //  will show the value of $tax
                                 // because we are using public function
                                 // to get the private value of  $tax  

2. Protected access Modifiers

This type of modifiers can be used in within class or child class, So it means protected properties are used in inheritance.

<?php 

Class MyClass{
    private $tax=10; //private property
    protected $parent="test" //protected property


    private function calculatePrice($amount){
        return  $this->tax+$amount;                 // we can only use private property within class
    }
    public function getTaxValue(){
        return  $this->tax;                 // private property within class
    }
    protected function check(){
           $this->tax;                 // private property within class
           $this->parent;                 // protected accessible within class
         
    }
}
}


class MyclassChild extends MyClass{
 
 
    public function callParent(){

        $this->check();         //  // protected accessible in child class
        $this->calculatePrice(); // will generate errror 

    }

     
}

$MyclassChild    =   new MyclassChild();
$MyclassChild->callParent();   // public method      

3. Public Access Modifiers

This type of modifiers can be used anywhere. They are not bounded to any class, child class or outside the class. So as you see in above example public methods and properties are accessible in class or outside the class as well.

Related

Softwares Javascript Php

Post navigation

Previous post
Next post

Related Posts

How to generate component in angular cli?

August 29, 2021August 29, 2021

Angular cli has rich features of generating components, providers, modules and many more. Angular cli can also be used for build production ready product and serve the project for local environment. Usage of Angular cli ng generate command To generate the component ng generate component component_name To generate the modules…

Read More

Show dropdown list from ajax response in jQuery

September 10, 2021September 10, 2021

Showing Dynamic Select Box using jQuery and Php,MySql In this tutorial, we are going to learn the dropdown list using AJAX. Loading dropdown values from AJAX requires knowledge of javascript, jQuery, php and html. Sometimes it requires to load the dropdown like countries, state, city list dependently. In this case…

Read More

jQuery redirect after Ajax success Laravel PHP

December 4, 2021December 4, 2021

In this article i will show you to redirect a page after successful response from Ajax request. Laravel or PHP provides itself to redirect a page using its own methods but while working with JavaScript Ajax Laravel methods are not useful because it won’t redirect the page. so in this…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version