Q1. What is the difference between a class and an object?
A class is a blueprint. An object is an instance created from it with new.
Q2. What are public, protected and private?
public: anywhere. protected: the class and its subclasses. private: only the class itself. Start with private.
Q3. What is a constructor? What is constructor promotion?
__construct() runs when an object is created. Promotion (PHP 8.0) declares and assigns properties in the constructor signature.
<?php
class User
{
public function __construct(public string $name, private int $age = 18) {}
public function describe(): string
{
return "{$this->name}, {$this->age}";
}
}
echo (new User("Ana"))->describe();
Ana, 18
Q4. What is the difference between an abstract class and an interface?
An abstract class can contain properties and implemented methods, and a class can extend only one. An interface only declares public method signatures (and constants), and a class can implement many. Use an interface for a contract, an abstract class for shared code.
Q5. What is a trait?
A way to share methods and properties between unrelated classes (PHP has single inheritance). Conflicts are resolved with insteadof and as. A trait is not a type.
Q6. What is the difference between self:: and static::?
self:: refers to the class where the code is written. static:: uses late static binding: it refers to the class that was actually called.
<?php
class A
{
public static function make(): string { return static::class; }
public static function makeSelf(): string { return self::class; }
}
class B extends A {}
echo B::make() . " " . B::makeSelf();
B A
Q7. What does final do?
A final class cannot be extended. A final method cannot be overridden.
Q8. What is polymorphism?
Different classes responding to the same method call in their own way, so the caller can work with a parent type or an interface.
Q9. What is encapsulation?
Keeping an object’s data private and exposing a small set of methods, so the object protects its own rules (invariants).
Q10. What is dependency injection?
Giving a class the objects it needs (usually through the constructor) instead of creating them inside. It makes code flexible and testable.
Q11. Composition or inheritance?
Prefer composition (“has a”). Use inheritance for a true “is a” relationship with a stable parent, and when a framework asks for it.
Q12. What are magic methods? Name some.
Special methods called automatically: __construct, __destruct, __toString, __get, __set, __call, __callStatic, __invoke, __clone, __isset.
Q13. How are objects compared?
== compares class and property values. === checks that both variables point to the same instance.
<?php
$a = new stdClass();
$b = new stdClass();
$c = $a;
var_dump($a == $b, $a === $b, $a === $c);
bool(true)
bool(false)
bool(true)
Q14. What happens when you assign an object to another variable?
You copy the handle, not the object. Both variables refer to the same object. Use clone for a copy (and __clone() for a deep copy).
Q15. What are readonly properties and enums?
A readonly property (PHP 8.1) can be set once and then not changed. An enum (PHP 8.1) is a type with a fixed set of cases, optionally backed by a string or int value (Status::tryFrom("paid")).
Q16. What is a namespace for?
To avoid name clashes and to organise code. Combined with PSR-4 autoloading, a class name maps to a file path.