Field Manual PHP / Laravel reference
← PHP

Visibility

public, protected, and private control access to members.

What it is

public, protected, and private control access to members.

When to use it

Keep internal state private and expose deliberate operations.

Syntax

public function balance(): int {}
protected function validate(): void {}
private int $balanceCents = 0;

Example

class Wallet
{
    private int $balanceCents = 0;
    public function credit(int $cents): void
    {
        if ($cents <= 0) throw new InvalidArgumentException("Positive credit required");
        $this->balanceCents += $cents;
    }
    public function balance(): int { return $this->balanceCents; }
}

Common mistakes

Public balance setters bypass business rules; visibility is not user authorization.

Related topics

Browse PHP