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

Javascript How to create global variable in angular ?

How to create global variables in angular ?

October 3, 2022March 16, 2024

In this article, we will learn about the create global variables in Angular and how to use them. We will also see how to use them across the application by importing the file. There are different ways to create global variables in angular. One way is to use the global…

Read More
Php Laravel session

How to set and get session in Laravel 8?

November 8, 2021November 17, 2021

Laravel has its own class to manage the session using the file system, redis, array, cookie and database. Session are used to keep the information about the user across the multiple request. As we know PHP is a server side language and all information lost after the response, so keep…

Read More
Php Laravel 8 ajax form validation

Laravel 8 Ajax form validation with example

October 20, 2021November 16, 2021

In this article, I will demonstrate to show Laravel server side validation using Ajax. We will use jQuery to show the validation and submit the form. Using the jQuery and AJAX we will prevent the page reloading and show the validation. In this article, we will use the same simple…

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

  • July 2025
  • 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

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version