ZetCode

PHP public Keyword

last modified November 25, 2026

The PHP public keyword makes a property, method, or constant reachable from any scope. It is the default visibility for methods and class constants, and it is the level that defines what the rest of the program may call.

This article shows public properties, public methods over private state, public constants and static members, the rules for changing visibility in a subclass, and public methods that implement an interface.

Basic definitions

The public keyword is a visibility modifier for properties, methods, and class constants. A public member can be used from any scope: inside the class, inside a subclass, inside an unrelated class, and in ordinary script code.

PHP has three visibility levels. public is the least restrictive, protected limits a member to the class and its subclasses, and private limits it to the declaring class. Methods, class constants, and static properties are public when no modifier is written; an instance property needs an explicit modifier (or the older var keyword), because a bare $name; is a parse error.

Public members form the API of a class. Every public property, method, and constant is a promise to the rest of the program, so the keyword is best used for operations the class is designed to support, not for every member that happens to be convenient.

A public property

The first example declares a public property and assigns it from outside the class.

pub_property.php
<?php

declare(strict_types=1);

class User
{
    public string $name = "guest";
}

$user = new User();
$user->name = "Alice";

echo "User: ", $user->name, "\n";

The script creates a User, replaces the default name, and reads it back. A public property can be read and written from anywhere, and the type declaration still applies: with strict types enabled, assigning an array or an integer to $name raises a TypeError. The trade-off is that no method can validate the new value or react to the change.

$ php pub_property.php
User: Alice

A public method over private state

A public method can expose an operation while the state behind it stays private. The class then decides what may change and when.

pub_method.php
<?php

declare(strict_types=1);

class Cart
{
    private array $items = [];

    public function add(string $item): void
    {
        $this->items[] = $item;
    }

    public function count(): int
    {
        return count($this->items);
    }
}

$cart = new Cart();
$cart->add("book");
$cart->add("pen");

echo "Items: ", $cart->count(), "\n";

The basket is stored in a private array, and the only way to add an item is the public add method. count is public because callers need the size. If the storage later changes to an associative array or an object, the two public methods can hide the difference; code that had read a public array directly would have to change with it.

$ php pub_method.php
Items: 2

Public constants and static members

Class constants and static properties accept visibility as well. Both are reached through the class name, and both are public unless a narrower modifier is written.

pub_static.php
<?php

declare(strict_types=1);

class Counter
{
    public const LIMIT = 3;
    public static int $created = 0;

    public function __construct()
    {
        self::$created++;
    }
}

new Counter();
new Counter();

echo Counter::$created, " of ", Counter::LIMIT, "\n";

Counter::$created belongs to the class, not to an object, and the constructor increments it with self::$created++. LIMIT is a public constant, so outside code can read it but cannot assign to it. A public static property is mutable global state: any code can set it to any value, which is why many designs keep the counter private and expose a public method instead.

$ php pub_static.php
2 of 3

Widening visibility in a subclass

A subclass may make an inherited protected method public. The method then becomes part of the child's public API while the parent keeps its own restrictions.

pub_inherit.php
<?php

declare(strict_types=1);

class Shape
{
    protected function label(): string
    {
        return "shape";
    }

    public function describe(): string
    {
        return "a " . $this->label();
    }
}

class Square extends Shape
{
    public function label(): string
    {
        return "square";
    }
}

$square = new Square();
echo $square->describe(), "\n";
echo $square->label(), "\n";

Shape::describe is public and calls the protected label hook. Square widens label to public, so the override is reachable both through describe and directly. The parent class is unchanged: a variable typed as Shape still cannot call label, because visibility is checked against the class the call is written for.

$ php pub_inherit.php
a square
square

Narrowing visibility is a fatal error

The reverse direction is not allowed. A child cannot hide a public method by declaring it protected or private.

pub_narrow.php
<?php

declare(strict_types=1);

class Shape
{
    public function describe(): string
    {
        return "shape";
    }
}

class Square extends Shape
{
    protected function describe(): string
    {
        return "square";
    }
}

PHP rejects the class at compile time with Access level to Square::describe() must be public (as in class Shape). If such a class were accepted, code holding a Shape could call describe and end up in a method it is not allowed to call. Widening is safe, narrowing is not, so only widening is permitted.

$ php pub_narrow.php
Fatal error: Access level to Square::describe() must be public (as in class Shape) in /app/pub_narrow.php on line 15
Stack trace:
#0 {main}

Interface methods are public

Every method declared in an interface is public, and an implementing class must declare it public as well. The interface is the clearest example of a public contract.

pub_interface.php
<?php

declare(strict_types=1);

interface Notifier
{
    public function send(string $message): string;
}

class EmailNotifier implements Notifier
{
    public function send(string $message): string
    {
        return "email: " . $message;
    }
}

function notify(Notifier $notifier): void
{
    echo $notifier->send("Build finished"), "\n";
}

notify(new EmailNotifier());

The interface fixes the signature, the class supplies the body, and notify depends only on the interface. A protected implementation would not satisfy the contract, because callers reach the method through the interface type. This is also why an interface is a good place to document the public API of a library: it lists the methods without showing how they work.

$ php pub_interface.php
email: Build finished

Best practices

A small public surface is easier to keep compatible, and every public member deserves a second thought.

Source

PHP manual: visibility

PHP manual: class constants

PHP manual: static keyword

This article covered the PHP public keyword: public properties, methods, constants, and static members, widening visibility in a subclass, the error raised by narrowing it, and public interface methods. Continue with PHP readonly keyword for properties that are set once, or review PHP protected keyword for the level that subclasses share.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all PHP tutorials.