🐘 PHP Beginner

What is a PHP class and how do you create one?

Why Interviewers Ask This

Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid PHP basics — a prerequisite for any developer role.

Answer

A class in PHP is a blueprint that defines the structure and behavior of objects. Define a class with the class keyword, then instantiate it with new: class Car { public string $color; public function __construct(string $color) { $this->color = $color; } public function getColor(): string { return $this->color; } }. Create an instance: $car = new Car("red");. Access properties and methods with the arrow operator (->): $car->getColor(). Static members are accessed with the double-colon (::): Car::$count or Car::create(). PHP classes also support constructors, destructors, magic methods, and interfaces.

Common Mistake

A common mistake is memorizing definitions without understanding implications. When asked this question, go one level deeper — explain what happens when this concept is misused or ignored.