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 html string in blade in laravel

How to Render Html String in Blade in Laravel ?

March 8, 2022December 2, 2023

Blade is a template engine to write the syntax easily and powerfully. To render html string in blade in laravel we use {!! $htmlstring !!}. Using the blade template we can easily print a variable, can create loops and can create directives and components too. Laravel blade template uses filename.blade.php…

Read More
Php Laravel min in eloquent query

Laravel min in eloquent query with example

January 23, 2022January 23, 2022

In this article we will learn to use aggregate function min in laravel eloquent and query builder. Laravel itself provide inbuilt method to min the columns like in MySQL or SQL based database we can do min of columns using min aggregate function. Min method will give you the most…

Read More
Php How to Restore Soft Deleted Records in Laravel 9

How to Restore Soft Deleted Records in Laravel 9 ?

June 16, 2022June 15, 2022

In this article we will learn to restore soft deleted records in Laravel. In our recent article How to fetch Soft Deleted Records in Laravel 9 ? we learnt to fetch the records from database which is soft deleted and sometimes we want to restore records that are soft deleted…

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

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

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version