Launch offer
Business website $150 USD Custom plugin $200 USD Ready in 5 days
Get a quote
OOP

Public, Private and Protected

Choose the right visibility for each property and method.

Public, Private and Protected — a practical guide to public private protected PHP with clear examples you can reuse in real projects.

OOP Tutorial Series (14/65). Prefer one article? Read the complete OOP tutorial.

Short description

Default to private/protected for state; expose only what callers need via public methods.

Visibility example

class BankAccount {
    private float $balance = 0;
    public function deposit(float $amount): void {
        if ($amount <= 0) throw new InvalidArgumentException('Invalid amount');
        $this->balance += $amount;
    }
}

Leave a reply

Your email address will not be published. Required fields are marked *