Lesson 25 / 25
Revision and Interview Questions
Recall PHP concepts quickly for exams and interviews.
Cheat sheet
Model: server-side, shared-nothing requests; NGINX + PHP-FPM; front controller in public/index.php; CLI for scripts and workers. Basics: declare(strict_types=1); scalar and compound types; === over ==; ?? and ??=; single vs double quotes. Control: match (strict, expression, no fall-through) vs switch; foreach and reference pitfalls. Functions: typed parameters and returns, named arguments, variadics and spread, closures with use, arrow functions (auto capture by value), first-class callables, never, nullsafe ?->. Arrays and strings: ordered maps, array_map/filter/reduce/column, usort with <=>, array_values after filtering, mb_* for UTF-8. OOP: constructor promotion, readonly properties and classes, enums (pure and backed, from/tryFrom), interfaces, abstract classes, final, traits, #[\Override], attributes, PHP 8.4 property hooks and asymmetric visibility. Errors: Throwable, Exception vs Error, chaining with previous, JSON_THROW_ON_ERROR, never display errors in production. Types and tools: union, intersection, DNF; PHPStan/Psalm; PSR-4 autoloading; Composer, composer.lock, composer audit. Web: superglobals, validation, sessions with secure cookies and session_regenerate_id; XSS escaping, CSRF tokens, prepared statements, password_hash. Ecosystem: PSR-3/7/15/18, Laravel and Symfony, PHPUnit/Pest, Rector, OPcache, deployment with immutable images.
Common interview questions
Answer each with a short code example.
1. What does declare(strict_types=1) do, and why use it?
2. What is the difference between == and ===? Give a surprising example.
3. Compare match and switch.
4. Explain constructor property promotion and readonly properties.
5. What are enums in PHP 8.1, and when would you use a backed enum?
6. Interfaces vs abstract classes vs traits: when would you use each?
7. How do PSR-4 autoloading and Composer work together?
8. How do you prevent SQL injection, XSS and CSRF in PHP?
9. How should passwords be stored and verified?
10. Exception vs Error in PHP 8: what is the difference?
11. What is OPcache and why does it matter?
12. How would you modernise a legacy PHP codebase safely?Show modern PHP in answers
Interviewers still meet candidates who learned PHP 5. Writing a small typed class with constructor promotion, a backed enum and a prepared statement immediately shows you know the current language.
Quick check: Which feature lets you declare and assign properties directly in the constructor signature?
- Traits
- Magic methods
- Late static binding
- Constructor property promotion
Answer
Constructor property promotion — Constructor promotion (PHP 8.0) combines property declaration and assignment.