dots Created with Sketch.
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.

amazon

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

#Jul 22 Is there a CSS parent selector? #Jul 22 Difference between :where and :is in CSS #Jul 22 Does :is() pseudo selector hint at CSS preprocessing in the future? #Jul 22 Control Scrolling with CSS Scroll Snap #Jul 21 Control rendering using CSS content-visibility property #Jul 21 How to use @supports rule in CSS?
You might also like these
LIMIT and OFFSET in PostgresPostgresINNER JOIN in PostgresPostgresEffects in ReactReactHow states work in React?ReactSolution to “Call to undefined function mysql_error()” in RevSlider WordPress PluginWordPressSQL GROUP BY StatementSQL/MySQL