How to Pass a PHP Interview: Top Advanced-Level Questions
\n\nAn interview for a Senior PHP Developer position is a test not only of syntax, but also of the depth of understanding of architecture, performance, and modern standards. In this article, we will break down advanced questions that are often asked in technical interviews and provide detailed answers with code examples. The article will be useful both for preparing for an interview and for improving your skills.
\n\n1. SOLID Principles in PHP: From Theory to Practice
\n\nOne of the most popular blocks of questions is knowledge and the ability to apply SOLID principles. The interviewer wants to see how you design flexible and maintainable architecture.
\n\nQuestion: "Tell us about the Open/Closed Principle (OCP) using PHP as an example."
\n\nAnswer: Classes should be open for extension but closed for modification. Instead of changing existing code, we add new code.
\n\nExample of violating OCP:
\n<?php\nclass ReportGenerator {\n public function generate($type) {\n if ($type === 'pdf') {\n // PDF generation\n } elseif ($type === 'csv') {\n // CSV generation\n }\n }\n}\n\n\nSolution adhering to OCP:
\n<?php\ninterface ReportInterface {\n public function generate(): string;\n}\n\nclass PdfReport implements ReportInterface {\n public function generate(): string {\n return 'PDF content';\n }\n}\n\nclass CsvReport implements ReportInterface {\n public function generate(): string {\n return 'CSV content';\n }\n}\n\nclass ReportGenerator {\n public function generate(ReportInterface $report): string {\n return $report->generate();\n }\n}\n\n\nThis approach allows adding new report formats without changing the existing ReportGenerator code.
2. Design Patterns: Singleton, Factory, and Dependency Injection
\n\nKnowledge of patterns is a mandatory requirement for a senior position. The most frequently asked about are Singleton, Factory, and Dependency Injection.
\n\nQuestion: "Implement the Singleton pattern in PHP and explain when to use it."
\n\nAnswer: Singleton guarantees that a class has only one instance. It is used for database connections, loggers, or configuration.
\n\n<?php\nclass Database {\n private static $instance = null;\n private $connection;\n\n private function __construct() {\n $this->connection = new PDO('mysql:host=localhost;dbname=test', 'user', 'pass');\n }\n\n public static function getInstance(): self {\n if (self::$instance === null) {\n self::$instance = new self();\n }\n return self::$instance;\n }\n\n public function getConnection(): PDO {\n return $this->connection;\n }\n\n // Disallow cloning and deserialization\n private function __clone() {}\n public function __wakeup() {\n throw new \\Exception("Cannot unserialize singleton");\n }\n}\n\n// Usage\n$db = Database::getInstance();\n\n\nQuestion: "What is Dependency Injection and how to implement it without a framework?"
\n\nAnswer: DI is passing dependencies into a class via the constructor or setters, rather than creating them inside the class.
\n\n<?php\nclass Logger {\n public function log(string $message): void {\n echo $message;\n }\n}\n\nclass UserService {\n private Logger $logger;\n\n // Dependency injection via constructor\n public function __construct(Logger $logger) {\n $this->logger = $logger;\n }\n\n public function createUser(array $data): void {\n // user creation logic\n $this->logger->log('User created');\n }\n}\n\n// Usage\n$logger =