Inheritance — PHP Technical Interview Questions & Answers
Inheritance
Inheritance
- Inheritance is a PHP topic. In plain words you use it for class Student in a small OOP file. Don’t start with a slogan — start with that picture.
- Smallest example: new Student('Asha', 72). Type it, run it, and say what you see. If you can do that from memory, you know Inheritance.
- From the example next to this theory: Inheritance: scores array, print avg, then high marks.
- Trap: public everything. Fix that before you talk about advanced PHP.
- Viva: what is Inheritance? Then show new Student('Asha', 72). Then name the trap.
Technical Interview Tip: What is Inheritance? Show this: new Student('Asha', 72). Trap: public everything.
Code Examples for Inheritance
Inheritance — working example
<?php
// Inheritance
$scores = [80, 90, 70];
$avg = array_sum($scores) / count($scores);
echo "avg=" . number_format($avg, 1) . "\n";
foreach ($scores as $s) {
if ($s >= 80) echo "high: $s\n";
}Inheritance: scores array, print avg, then high marks. Say the output.
How to Explain Inheritance to an Interviewer
What is Inheritance?
Used for class Student a small OOP file Not just the heading
Give one small example of Inheritance.
new Student('Asha', 72) Inheritance: scores array, print avg, then high marks
What mistake do beginners make with Inheritance?
public everything Show the fix
Where do you use Inheritance?
a small OOP file class Student