Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In PHP, declare a class with the class keyword, define its properties and methods inside braces, then create an object with new. The example below defines a User class, initializes its name in a constructor, and exposes a public greeting method.

Write a class and create an object

A class is a reusable definition for an object’s state and behavior. Properties hold state, methods define behavior, and a class can also contain constants. An object—also called an instance—is created with the new operator, as the PHP language specification explains.

<?php
class User
{
    private string $name;

    public function __construct(string $name)
    {
        $this->name = $name;
    }

    public function greeting(): string
    {
        return "Hello, {$this->name}";
    }
}

$user = new User('Ada');
echo $user->greeting();

This prints Hello, Ada. The new User('Ada') expression creates an instance and passes 'Ada' to __construct(). A constructor is the special method used to initialize an instance immediately after it is created, according to the PHP language specification.

What each part does

  • class User declares a class named User.
  • private string $name declares a typed property for storing each object’s name and restricts direct access to the class itself.
  • public function __construct(string $name) accepts a string when an object is created and assigns it to the property.
  • $this->name refers to the current object’s property. The PHP manual documents using $this and -> to access properties inside non-static methods: PHP properties.
  • public function greeting(): string defines a callable method that returns a string.
  • $user->greeting() calls that method on the object stored in $user.

Choose property and method visibility

Visibility controls which code can access a class member. It is part of a class’s API: expose what callers need, and keep implementation details restricted.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Visibility Who can access it Typical use
public Code everywhere Methods or properties intended for callers
protected The declaring class and its inheritance chain Behavior or state designed for a class and its subclasses
private Only the class that declared the member Internal implementation details

The PHP visibility manual states that class members declared public can be accessed everywhere. In the example, callers can invoke greeting(), but cannot read $name directly because it is private. A class can instead provide a public method when outside code needs a controlled way to use private state.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Decide when to use extends, implements, or traits

Use extends for a genuine specialization

A child class can extend a parent class to specialize it and reuse its behavior. A subclass inherits public and protected methods, properties, and constants unless it overrides them; private parent methods are not accessible to the child. See the PHP inheritance manual.

class Admin extends User
{
    public function dashboardLabel(): string
    {
        return 'Admin dashboard';
    }
}

This is appropriate only when an Admin is meaningfully a kind of User. Inheritance couples the child to its parent; do not use it solely to share a convenient method.

Use implements for a shared contract

An interface specifies methods a class must provide without requiring that classes share one implementation. Different classes can therefore be used interchangeably where code depends on the interface rather than on a particular class. The PHP interface documentation describes this contract-based use.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
interface Greeter
{
    public function greeting(): string;
}

class User implements Greeter
{
    public function greeting(): string
    {
        return 'Hello';
    }
}

Use a trait to share methods

A trait provides reusable methods that can be included in otherwise unrelated classes. It is a code-reuse mechanism, not a parent-child relationship or an interface contract. Traits are covered alongside inheritance and interfaces in the PHP Classes and Objects documentation.

Make the design decisions in the right order

  1. Identify the object’s state and behavior. Put per-object data in properties and operations on that data in methods.
  2. Decide what callers need. Make only intended entry points public; keep internal state private unless subclasses need protected access.
  3. Define required initialization. Accept required values in __construct() and assign or validate them there.
  4. Choose a reuse mechanism based on the relationship. Use extends for specialization, implements for a contract, and a trait to share methods across unrelated classes.
  5. Instantiate and use the object. Call new ClassName(...), then invoke public methods with ->.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.