Classes, Objects & Constructor Promotion
A class is a blueprint; an object is an instance of that blueprint. PHP 8.0 introduced constructor property promotion — a major quality-of-life improvement that eliminates the verbose property declaration + assignment pattern. Modern PHP classes are concise, typed, and expressive.
Class Anatomy — Modern PHP 8.x
<?php
declare(strict_types=1);
// ── Classic class (PHP 7 style — verbose) ──────────────────────
class PostOld {
private int $id;
private string $title;
private string $author;
private float $price;
public function __construct(int $id, string $title, string $author, float $price) {
$this->id = $id;
$this->title = $title;
$this->author = $author;
$this->price = $price;
}
// getters...
}
// ── Constructor property promotion (PHP 8.0+) — same result, less code ──
class Post {
public function __construct(
private readonly int $id,
private string $title,
private string $author,
private string $slug,
private string $content = "",
private bool $published = false,
private ?DateTimeImmutable $publishedAt = null,
) {}
// ── Getters (no setters for readonly/immutable design) ────────
public function getId(): int { return $this->id; }
public function getTitle(): string { return $this->title; }
public function getSlug(): string { return $this->slug; }
public function isPublished(): bool { return $this->published; }
// ── Setters return new instance — immutable pattern ──────────
public function withTitle(string $title): static {
$clone = clone $this;
$clone->title = $title;
return $clone;
}
public function publish(): static {
$clone = clone $this;
$clone->published = true;
$clone->publishedAt = new DateTimeImmutable("now", new DateTimeZone("UTC"));
return $clone;
}
// ── toString ────────────────────────────────────────────────
public function __toString(): string {
return sprintf("[Post #%d] %s by %s", $this->id, $this->title, $this->author);
}
// ── toArray — useful for JSON output ─────────────────────────
public function toArray(): array {
return [
"id" => $this->id,
"title" => $this->title,
"slug" => $this->slug,
"author" => $this->author,
"published" => $this->published,
"published_at" => $this->publishedAt?->format("c"),
];
}
}
// Usage
$post = new Post(1, "PHP Basics", "Alice", "php-basics");
echo $post; // [Post #1] PHP Basics by Alice
$published = $post->publish(); // original unchanged
var_dump($post->isPublished()); // false
var_dump($published->isPublished()); // true
echo json_encode($post->toArray(), JSON_PRETTY_PRINT);Quick Quiz
Tip
Tip
Practice Classes Objects Constructor Promotion in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
PHP processes each request through the server-side engine
Practice Task
Note
Practice Task — (1) Write a working example of Classes Objects Constructor Promotion from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Classes Objects Constructor Promotion is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.