public
The "public" keyword is used in PHP to declare a class member as public, meaning that it can be accessed from anywhere in your code. In this article, we will
The PHP public Keyword
public is one of PHP's three visibility (access) modifiers, alongside private and protected. A class member declared public can be read and written — or, for methods, called — from anywhere: inside the class, from a subclass, and from any code that holds an object of the class.
This page covers the syntax, where public applies, how it differs from the other modifiers, the default-visibility rules, and the practical gotchas of exposing members publicly. If classes are new to you, start with PHP Classes and Objects.
Syntax
Place the public keyword before a property, method, or constant declaration:
class MyClass {
public $myPublicProperty; // public property
public const VERSION = '1.0'; // public constant (PHP 7.1+)
public function myPublicMethod() {
// accessible from anywhere
}
}public can modify:
- Properties —
public $name; - Methods —
public function greet() { ... } - Constants —
public const MAX = 10;(PHP 7.1+; constants are implicitly public if no modifier is given) - Constructor-promoted parameters —
public function __construct(public string $name) {}(PHP 8.0+)
Default visibility
Visibility is optional in PHP. The default depends on the member type:
- A method with no modifier is implicitly
public.function honk()andpublic function honk()mean the same thing. - A property must use one of the modifiers (
public,protected, orprivate) orvar. Historicallyvar $x;was an alias forpublic $x;and is still accepted but discouraged. - A class constant with no modifier is implicitly
public.
Writing public explicitly is the recommended style — it makes the intent obvious to anyone reading the code.
Examples
Let's look at some practical examples of how the "public" keyword can be used:
Examples of PHP public keyword
<?php
// Example 1
class Car
{
public $model;
public $color;
public function __construct($model, $color)
{
$this->model = $model . PHP_EOL;
$this->color = $color;
}
public function honk()
{
return "Beep beep!" . PHP_EOL;
}
}
$myCar = new Car("Tesla", "red");
echo $myCar->model; // Output: Tesla
echo $myCar->honk(); // Output: Beep beep!
// Example 2
class Math
{
public static function add($a, $b)
{
return $a + $b;
}
public static function multiply($a, $b)
{
return $a * $b;
}
}
echo Math::add(5, 10); // Output: 15
echo Math::multiply(5, 10); // Output: 50Here, $model, honk(), and the static Math methods are all public, so the calling code reaches them directly through -> (instances) or :: (static members).
Constructor promotion (PHP 8.0+)
Since PHP 8.0 you can declare and initialize a public property straight from a constructor parameter, removing the repetitive $this->x = $x; boilerplate:
<?php
class Point
{
public function __construct(
public int $x = 0,
public int $y = 0
) {}
}
$p = new Point(3, 4);
echo "$p->x,$p->y" . PHP_EOL; // Output: 3,4This is equivalent to declaring public int $x; and assigning it in the constructor body. See PHP Constructor for the full picture.
public vs. protected vs. private
PHP has three visibility modifiers. The difference is where a member can be accessed from:
| Modifier | Same class | Subclass | Outside code |
|---|---|---|---|
public | yes | yes | yes |
protected | yes | yes | no |
private | yes | no | no |
The following script shows the boundaries in action:
<?php
class Base
{
public $open = 'public';
protected $family = 'protected';
private $secret = 'private';
public function reveal()
{
// Inside the class, all three are reachable.
return "$this->open / $this->family / $this->secret" . PHP_EOL;
}
}
$b = new Base();
echo $b->open . PHP_EOL; // Output: public (allowed from outside)
echo $b->reveal(); // Output: public / protected / private
// echo $b->family; // Fatal error: Cannot access protected property
// echo $b->secret; // Fatal error: Cannot access private propertyOnly $open is reachable directly from outside the class. $family and $secret can only be read through the public reveal() method.
When to use public
Use public for the parts of a class that form its API — the methods and data you want other code to rely on. Keep everything else private or protected.
- Expose behavior, not raw state. Prefer
publicmethods (getEmail(),withdraw($amount)) overpublicproperties, so you keep control over validation and can change the internals later without breaking callers. - A
publicproperty is a contract: once code outside depends on it, renaming or removing it is a breaking change. - Use
public staticfor utility/factory methods that do not need an instance (see PHP Static Methods). - Interface methods are always effectively public — see PHP Interfaces.
Summary
publicmakes a class member accessible from anywhere; it is the most permissive of PHP's three visibility modifiers.- Methods and constants are
publicby default; properties must state their visibility explicitly. - Reserve
publicfor the intended API of a class and hide implementation details behindprivate/protectedfor better encapsulation.
Continue with private, protected, and PHP Inheritance to round out your understanding of visibility.