W3docs

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:

  • Propertiespublic $name;
  • Methodspublic function greet() { ... }
  • Constantspublic const MAX = 10; (PHP 7.1+; constants are implicitly public if no modifier is given)
  • Constructor-promoted parameterspublic 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() and public function honk() mean the same thing.
  • A property must use one of the modifiers (public, protected, or private) or var. Historically var $x; was an alias for public $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: 50

Here, $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,4

This 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:

ModifierSame classSubclassOutside code
publicyesyesyes
protectedyesyesno
privateyesnono

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 property

Only $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 public methods (getEmail(), withdraw($amount)) over public properties, so you keep control over validation and can change the internals later without breaking callers.
  • A public property is a contract: once code outside depends on it, renaming or removing it is a breaking change.
  • Use public static for utility/factory methods that do not need an instance (see PHP Static Methods).
  • Interface methods are always effectively public — see PHP Interfaces.

Summary

  • public makes a class member accessible from anywhere; it is the most permissive of PHP's three visibility modifiers.
  • Methods and constants are public by default; properties must state their visibility explicitly.
  • Reserve public for the intended API of a class and hide implementation details behind private/protected for better encapsulation.

Continue with private, protected, and PHP Inheritance to round out your understanding of visibility.

Practice

Practice
In PHP, what does the term 'public' denote when used with properties and methods of a class?
In PHP, what does the term 'public' denote when used with properties and methods of a class?
Was this page helpful?