yoklainterview sim

Backend Php Generics Interfaces Interview Questions

75 verified Backend Php Generics Interfaces interview questions — solve with answers, learn from explanations, test yourself in a real simulation.

Try the real simulation →

Sample questions

Php Generics InterfacesDifficulty 1
interface Greetable {
    public function greet(): string;
}

class Person implements Greetable {
}

What happens when this code is loaded?
  • aNothing, PHP interfaces are optional hints and are ignored at runtime.
  • bA fatal error occurs because Person does not implement the required greet() method.
  • cPerson compiles fine; greet() is auto-generated with an empty body.
  • dA warning is printed but the class can still be instantiated normally.
Explanation:A class implementing an interface must define every method the interface declares, or be declared abstract itself. Since Person implements Greetable without providing greet(), PHP raises a fatal error at class-definition time ("Class Person contains 1 abstract method and must be declared abstract or implement the remaining methods").
Php Generics InterfacesDifficulty 1
In PHP, can an abstract class be instantiated directly with new?
  • aYes, as long as it has a public constructor.
  • bNo, attempting new on an abstract class always raises a fatal error.
  • cYes, but only if all its methods have implementations.
  • dNo, but it can be instantiated dynamically via reflection without restriction.
Explanation:Abstract classes represent incomplete blueprints meant to be extended. PHP forbids instantiating them directly and throws a fatal error ("Cannot instantiate abstract class"), regardless of constructor visibility or whether every method happens to have a body.
Php Generics InterfacesDifficulty 1
Which of the following can a plain PHP interface declare?
  • aMethod signatures and typed instance properties.
  • bPrivate methods with default implementations.
  • cStatic properties shared by all implementing classes.
  • dMethod signatures and class constants only.
Explanation:Interfaces only declare method signatures (no body) and constants; they cannot declare instance or static properties. Implementation state always lives in the classes that implement the interface, never in the interface itself.
Php Generics InterfacesDifficulty 1
You add a Logger trait to a Service class in your app:
trait Logger {
    public function log(string $msg): void {
        echo "[LOG] $msg";
    }
}

class Service {
    use Logger;
}

(new Service())->log("ready");

What is printed?
  • aNothing, traits cannot be used inside classes without extends.
  • bFatal error: call to undefined method log().
  • cLOG ready (without brackets).
  • d[LOG] ready
Explanation:use Logger; copies the trait's method bodies into Service as if they were written directly in the class. Calling log() on a Service instance therefore executes the trait's implementation and prints "[LOG] ready".
Php Generics InterfacesDifficulty 1
What distinguishes a "pure" PHP enum from a "backed" enum?
  • aA pure enum can only have one case; a backed enum can have many.
  • bA pure enum is declared with class; a backed enum is declared with enum.
  • cA pure enum cannot implement interfaces; only backed enums can.
  • dA pure enum's cases have no scalar value attached; a backed enum's cases each map to an int or string.
Explanation:enum Suit { case Hearts; } defines a pure enum — cases are just named singleton instances. enum Suit: string { case Hearts = 'H'; } defines a backed enum, where each case additionally carries a scalar value accessible via ->value. Both kinds can implement interfaces.
Php Generics InterfacesDifficulty 1
class Point {
    public function __construct(
        public readonly int $x,
        public readonly int $y,
    ) {}
}

$p = new Point(1, 2);
echo $p->x;

What is printed?
  • a2
  • bFatal error: cannot read readonly property.
  • cNothing, constructor promotion does not work with readonly.
  • d1
Explanation:Constructor property promotion combined with readonly declares and initializes $x and $y in one step. Reading a readonly property after it has been initialized works exactly like a normal property, so $p->x prints 1.

Test yourself against the 3300-question Backend bank.

Start interview