Public Access Modifier in PHP

In this article I explain public access modifier in PHP.
  • 1992


It is quite clear, the "public" access modifier achieves the highest level of accessibility. If you define your class properties and methods as "public", then it can be used anywhere in your PHP script.

Example of Public access modifier

In the following example, the class property and method of Myclass is set to be "public", and outside the class, an instance is created of Myclass. Then the "title" property is accessed by $obj and is assigned a "title" "ABCXYZ". Then

the value of the title property is "$obj".

An instance is accessed again to display it, and finally the class method disptitle() is called by $obj, that will also display the value of the "title" property of the instance of the class. Since this method has an access to the title property, this is happening because of the class property and the method is public.  


class Myclass


public $title;


public function DispTitle()


echo $this -> title;

echo "<br />";



$obj = new Myclass();

$obj->title = "ABCXYZ";

echo $obj->title;

echo "<br />";

echo $obj->DispTitle();




© 2020 DotNetHeaven. All rights reserved.