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

Php How to Change Date Format in Laravel

How to Change Date Format in Laravel ?

July 6, 2022November 17, 2023

In this blog we will learn to change date format in laravel. Laravel provides by default two timestamp created_at and updated_at, and there format are according to database format but sometimes we want to show different format across the entire application so In this tutorial we will learn to change…

Read More
Php laravel routing

Laravel 8 routing with example

October 5, 2021January 13, 2022

In this post we will learn to implement the Laravel 8 routing and how it’s different from Laravel other versions. Laravel routing is used to create the URL for each POST, GET requests and it works as a bridge between the controller or html view. In this article we will…

Read More
Php How to Use hasOne Relationship in Laravel

How to Use hasOne Relationship in Laravel with Example ?

February 27, 2022October 4, 2022

hasOne relationship in laravel is used to create the relation between two tables. hasOne means create the relation one to one. For example if a article has comments and we wanted to get one comment with the article details then we can use hasOne relationship or a user can have…

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

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version