PHP Interview Questions
Practise a short explanation followed by an example and a limitation. A strong answer connects language behaviour to the requirement it helps satisfy.
Your brief
When would you choose an interface? When callers need a stable capability that different collaborators can supply. Why keep output encoding near rendering? Different destinations require different encoding rules. Why not use a static property for shared stock? Ordinary web executions do not provide durable coordinated storage through that property. Why is a prepared query not a complete access-control system? Parameters separate values from SQL syntax but do not decide which actor may see the selected rows. Explain trade-offs rather than claiming one construct solves every problem.
Run it locally
Run this complete source locally with PHP 8.2 or newer. Save it using the filename shown and run the command below. PHP is not currently available in Bhylu’s browser playground. Apache and MySQL are not needed for an ordinary CLI example.
php -l interview-practice.php
php interview-practice.phpDownload this source as a local PHP lab file. Keep lab files separate from the Bhylu project and production data.
Worked solution
Show the worked solution
<?php
declare(strict_types=1);
interface QuantityView { public function quantity(): int; }
final class FixedQuantity implements QuantityView {
public function __construct(private int $value) {}
public function quantity(): int { return $this->value; }
}
function describeQuantity(QuantityView $view): string { return $view->quantity() . " units"; }
echo describeQuantity(new FixedQuantity(4)), "\n";
Expected result
4 unitsThe caller depends on a narrow read capability rather than one database or framework class. The example does not validate the business range in its fixture constructor; add that rule if FixedQuantity is meant to be a real domain value rather than a test adapter. Calling out that limitation is part of an accurate design explanation.
Extend and test
Provide a second implementation backed by a trusted report fixture and run the same caller test against both.
Check the official PHP manual for detailed version and API contracts.