Yogesh Chauhan's Blog

How to define visibility for a property in PHP?

in PHP on June 15, 2021

There are many programming languages that supports visibility (not necessarily using the same name) such as C++.

PHP introduced visibility in version 7.1.0 for a constant too. So, you can define a visibility for a property, a method or a constant.

The visibility can be defined by prefixing the property declaration with a specific keyword. There are 3 types of keywords available for visibility control.

  • public sets the visibility to public.
    • Members can be accessed anywhere in the file.
  • protected sets the visibility to class itself and the classes from that class.
    • Members can be accessed inside the parent or child class.
  • private sets the visibility to the class itself.
    • Members can not be accessed anywhere else other than the class defining them, not even by child classes.

members = a property, a method or a constant 

You MUST define class properties as public, protected or private. Even if you use ar to declare a property, the visibility will be defined as public.

Here’s a really simple example from PHP official docs:


<?php
  /**
   * Define MyClass
   */
  class MyClass
  {
      public $public = 'Public';
      protected $protected = 'Protected';
      private $private = 'Private';

      function printHello()
      {
          echo $this->public;
          echo $this->protected;
          echo $this->private;
      }
  }

  $obj = new MyClass();
  echo $obj->public; // Works
  echo $obj->protected; // Fatal Error
  echo $obj->private; // Fatal Error
  $obj->printHello(); // Shows Public, Protected and Private
?>


The code above will result in Fatal Error unless you remove the line that gives you Fatal Error.

You can redeclare the protected and public properties, but you can not redeclare private properties inside a class methods.


Let’s define another class extending the first one:


<?php
  /**
   * Define MyClass2
   */
  class MyClass2 extends MyClass
  {
      // We can redeclare the public and protected properties, but not private
      public $public = 'Public2';
      protected $protected = 'Protected2';

      function printHello()
      {
          echo $this->public;
          echo $this->protected;
          echo $this->private;
      }
  }

  $obj2 = new MyClass2();
  echo $obj2->public; // Works
  echo $obj2->protected; // Fatal Error
  echo $obj2->private; // Undefined
  $obj2->printHello(); // Shows Public2, Protected2, Undefined
?>


The same visibility rules applies to a method or a constant as well.


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Dec 4 What is Etrieve Flow? #Dec 2 The unique operator($) in Envision Basic #Nov 25 Steps to Install Microsoft SQL Server on a MacOS #Nov 11 What is DevOps? #Nov 10 The * arithmetic operator in Envision Basic #Nov 10 What are Big Data Clusters in SQL Server?
You might also like these
List of all Pseudo Elements in CSSCSSWhat happens when we add Numbers and Strings in JavaScript?JavaScriptHow to redirect or load a new document using JavaScript?JavaScriptFew more :nth-child examples in CSSCSSHow to create and store JSON objects in localStorage using JavaScript?JavaScriptClean Form Input With These PHP Functions Before Inserting into DatabasePHP