PHP Reference ยท Functions and Classes
PHP classes and objects
Reference for declaring PHP classes: class, new, $this, methods, final and readonly classes, anonymous classes, instanceof and the ::class constant.
A class is a blueprint that bundles data (properties) and behavior (methods). You create an object, an instance of the class, with new. Every object has its own property values but shares the class's methods.
Inside a method, $this is the object the method was called on. Class names are case-insensitive, but write them exactly as declared.
class Counter { private int $count = 0; public function increment(): static { $this->count++; return $this; } public function value(): int { return $this->count; } } echo (new Counter)->increment()->increment()->value(); // 2
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
[final | abstract | readonly] class Name [extends Parent] [implements A, B] { // constants, properties, methods, trait use } $object = new Name(...$arguments); $object = new class(...$arguments) extends Base implements Contract { ... };
| Part | Description |
|---|---|
final |
The class cannot be extended. |
abstract |
The class cannot be instantiated; see Abstract classes. |
readonly |
PHP 8.2+. Every declared property becomes readonly; see readonly. |
extends |
One parent class; see Inheritance. |
implements |
One or more interfaces; see Interfaces. |
$this |
The current object inside non-static methods. |
Name::class |
The fully qualified class name as a string, resolved at compile time. |
Return value of new: a new object of that class.
Errors: instantiating an undefined class, an abstract class, an interface, a trait or an enum throws Error. Calling an undefined method throws Error.
Usage notes
- Objects are handles. Assigning or passing an object copies the handle, not the object, so both variables see the same data. Use clone for an independent copy.
- Parentheses after
neware optional when there are no arguments:new Counterequalsnew Counter(). Since PHP 8.4 you can call a method on a fresh object without extra parentheses:new Counter()->value(). - One class per file named after the class is the convention, and what autoloading relies on.
- Prefer
static/selfreturn types for fluent methods that return$this. instanceoftests class, parent and interface membership;get_class()returns only the exact class.- Anonymous classes are handy for one-off implementations and test doubles. Their generated class name is not stable, so do not compare it.
readonly class(8.2) forbids dynamic properties and requires typed properties.- Dynamic properties (creating undeclared properties) are deprecated since PHP 8.2 for ordinary classes; declare your properties or extend
stdClass. - Case:
new pointworks, but PSR coding standards require the declared spelling.