OOP — PHP Technical Interview Questions & Answers

OOP

OOP

  • class Student { public $name; function __construct($name) { $this->name = $name; } } $s = new Student('Asha'); Object is one student. Class is the blueprint. $this is that object.
  • public/private/protected control who can touch fields. Keep fields private when you can.
  • Trap: $s.method() like JS. PHP is $s->method(). Trap: forgetting new.

Technical Interview Tip: new Student('Asha'); $s->name $this inside the class.

Code Examples for OOP

OOP — working example

<?php
class User {
  public function __construct(private string $name) {}
  public function hello(): string {
    return "Hello, {$this->name}";
  }
}
echo (new User("Ravi"))->hello();

OOP — constructor property promotion keeps simple classes short.

How to Explain OOP to an Interviewer

How do you make an object?

new ClassName() $obj->method()