class
Learn how the PHP class keyword defines classes: properties, methods, constructors, visibility, constants, static members, and inheritance with examples.
The PHP class Keyword
The class keyword defines a class — a blueprint that describes the data (properties) and behavior (methods) shared by a group of objects. You then create individual objects (also called instances) from that blueprint with the new operator. Classes are the foundation of object-oriented programming (OOP) in PHP: they let you group related state and logic into a single, reusable unit instead of scattering loose variables and functions.
This page covers how to declare a class, add properties and methods, initialize objects with a constructor, control access with visibility modifiers, share data with constants and static members, and reuse behavior through inheritance.
Syntax
A class declaration starts with the class keyword, followed by a name and a body enclosed in braces:
<?php
class MyClass {
// Properties and methods go here
}By convention class names use PascalCase (MyClass, BankAccount). A class name cannot be a PHP reserved word, and the body — even an empty one — must use braces. Defining a class does nothing on its own; it only becomes useful once you create an object from it.
Properties and methods
A property is a variable that belongs to an object. A method is a function that belongs to a class. Inside a method, the special variable $this refers to the current object, so $this->name reads (or writes) that object's name property:
<?php
class User {
public string $name; // a property
public function greet(): string { // a method
return "Hi, I'm {$this->name}";
}
}
$u = new User();
$u->name = "Ada"; // set the property on this object
echo $u->greet(); // Hi, I'm AdaNote the arrow -> (not .) to access members, and that you write $this->name, never $this->$name.
The constructor
Setting every property by hand is verbose and error-prone. The __construct() method runs automatically when an object is created, letting you require and assign initial values in one step. The arguments you pass to new are forwarded to the constructor:
<?php
class Car
{
public $make;
public $model;
public $year;
public function __construct($make, $model, $year)
{
$this->make = $make;
$this->model = $model;
$this->year = $year;
}
public function describe(): string
{
return "{$this->year} {$this->make} {$this->model}";
}
}
$myCar = new Car("Ford", "Mustang", 2022);
echo $myCar->describe(); // 2022 Ford MustangSee PHP constructor for more, including constructor property promotion.
Visibility
Every property and method has a visibility that controls where it can be accessed:
public— accessible from anywhere (the default if you omit it).protected— accessible only inside the class and its subclasses.private— accessible only inside the class that declares it.
Hiding internal state behind private and exposing it through methods is called encapsulation:
<?php
class BankAccount
{
private float $balance = 0; // cannot be touched directly from outside
public function deposit(float $amount): void
{
$this->balance += $amount;
}
public function getBalance(): float
{
return $this->balance;
}
}
$acc = new BankAccount();
$acc->deposit(100);
echo $acc->getBalance(); // 100More detail in PHP access modifiers.
Constants and static members
Use the const keyword for values that never change and the static keyword for data and methods that belong to the class itself rather than to any one object. Both are accessed with the scope-resolution operator :::
<?php
class Circle
{
const PI = 3.14159; // class constant
public static int $count = 0; // shared across all instances
public function __construct()
{
self::$count++; // increment the shared counter
}
}
new Circle();
new Circle();
echo Circle::PI; // 3.14159
echo PHP_EOL;
echo Circle::$count; // 2See PHP class constants and PHP static methods.
Inheritance
A class can extends another to reuse and specialize its behavior. The child inherits the parent's properties and methods, and may override them:
<?php
class Animal
{
public function speak(): string
{
return "The animal makes a sound.";
}
}
class Dog extends Animal
{
public function speak(): string // overrides the parent method
{
return "The dog barks.";
}
}
echo (new Animal())->speak(); // The animal makes a sound.
echo PHP_EOL;
echo (new Dog())->speak(); // The dog barks.For deeper coverage see PHP inheritance, and for classes that define a contract without implementation, abstract classes and interfaces.
Full example
The following runnable example combines a constructor with getter methods, and demonstrates inheritance with method overriding:
<?php
// Example 1
class Car
{
public $make;
public $model;
public $year;
public function __construct($make, $model, $year)
{
$this->make = $make;
$this->model = $model;
$this->year = $year;
}
public function getMake()
{
return $this->make;
}
public function getModel()
{
return $this->model;
}
public function getYear()
{
return $this->year;
}
}
$myCar = new Car("Ford", "Mustang", 2022);
echo "Make: " . $myCar->getMake() . PHP_EOL;
echo "Model: " . $myCar->getModel() . PHP_EOL;
echo "Year: " . $myCar->getYear() . PHP_EOL;
// Output:
// Make: Ford
// Model: Mustang
// Year: 2022
// Example 2
class Animal
{
public function speak()
{
echo "The animal speaks.";
}
}
class Dog extends Animal
{
public function speak()
{
echo "The dog barks.";
}
}
$myAnimal = new Animal();
$myDog = new Dog();
$myAnimal->speak();
$myDog->speak();
// Output:
// The animal speaks.
// The dog barks.Example 1 builds objects with a constructor and reads their state through getters; Example 2 shows a Dog subclass overriding the speak() method it inherits from Animal.
Summary
- The
classkeyword defines a blueprint; thenewoperator creates objects from it. - Properties hold an object's data, methods define its behavior, and
$thisrefers to the current object inside a method. __construct()initializes an object as it is created.public,protected, andprivatecontrol where members can be accessed.constandstaticbelong to the class itself; access them with::.extendslets one class inherit and override another's behavior.
For the broader picture of how these pieces fit together, see PHP classes and objects.