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 add Read More Read Less Button using JavaScript? #2 How to check if radio button is checked or not using JavaScript? #3 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
If statement shorthand examples in JavaScriptJavaScriptHow to verify your domain in Google Console with a TXT record?MiscellaneousHow to import MySQL small sample database into phpMySQL?SQL/MySQLHow to add Laravel to WordPress using Sage theme (and install Tailwind CSS)?PHPCSS align-items property examplesCSSKilling A Project Part 3: How can an organization ensure that a doomed project is killed as early as possible?Miscellaneous