PHP Interview Questions
In this page:
Interface vs. Abstract Class
Interfaces declare only method signatures with no implementation, forcing implementing classes to provide their own behavior; abstract classes can mix concrete methods with abstract ones, which is why interfaces suit unrelated classes sharing a contract while abstract classes suit closely related ones sharing code.
Example: Interface vs. Abstract Class
<?php
// Define the interface `Shape` describing required methods
interface Shape {
// Define the function `area` with no parameters
function area();
}
// Define the class `Vehicle`
abstract class Vehicle {
// Define the function `honk` with no parameters
function honk() { echo "Beep\n"; }
// Define the function `drive` with no parameters
abstract function drive();
}
// Print "Interfaces: contract only. Abstract classes: can mix concrete + abstract methods." to the output
echo "Interfaces: contract only. Abstract classes: can mix concrete + abstract methods.";
?>
Login to try C/C++/Java/PHP code in the editor
Dependency Inversion
Dependency inversion means code should depend on an interface or abstraction rather than a specific concrete class, so swapping one implementation for another (like switching database drivers) doesn't require touching the code that uses it.
Example: Dependency Inversion
<?php
// Define the interface `Logger` describing required methods
interface Logger {
// Define the function `log` taking `$msg`
function log($msg);
}
// Define the class `FileLogger`, implementing `Logger`
class FileLogger implements Logger {
// Define the function `log` taking `$msg`
function log($msg) { echo "File log: $msg"; }
}
// Define the class `App`
class App {
// Declare the private property `$logger`
private $logger;
// Define the constructor `__construct` taking `Logger $logger`
function __construct(Logger $logger) {
$this->logger = $logger;
}
// Define the function `run` with no parameters
function run() { $this->logger->log("App started"); }
}
(new App(new FileLogger()))->run();
?>
Login to try C/C++/Java/PHP code in the editor
Cookies vs. Sessions
Cookies live in the browser as plain text the user could inspect or tamper with, while sessions keep the actual data server-side and hand the browser only an opaque ID -- which is why sensitive data always belongs in a session, not a cookie.
Example: Cookies vs. Sessions
<?php
setcookie("theme", "dark"); // stored in browser, visible/tamperable
session_start();
$_SESSION['user_id'] = 5; // stored server-side, only ID given to browser
echo "Cookie: client-side. Session: server-side.";
?>
Login to try C/C++/Java/PHP code in the editor
Garbage Collection
PHP tracks how many references point to each value and frees memory once that count hits zero, with an additional cycle-collector that catches objects referencing each other in a loop that reference counting alone can't detect.
Example: Garbage Collection
<?php
class Node {
public $next;
}
$a = new Node();
$b = new Node();
$a->next = $b;
$b->next = $a; // circular reference
unset($a, $b);
gc_collect_cycles();
echo "Cycle collector reclaimed the circular reference";
?>
Login to try C/C++/Java/PHP code in the editor
SQL Injection Prevention
Prepared statements via PDO send the SQL structure and the user-supplied values to the database separately, so even a malicious input string can never be interpreted as part of the query itself -- the strongest defense against SQL injection.
Example: SQL Injection Prevention
<?php
// Create a new `PDO` instance with 'sqlite::memory:', stored in `$pdo`
$pdo = new PDO('sqlite::memory:');
$pdo->exec("CREATE TABLE users (id INTEGER, name TEXT)");
// Declare `$stmt`, set to `$pdo->prepare("INSERT INTO users (id, name) VALUES (?, ?)")`
$stmt = $pdo->prepare("INSERT INTO users (id, name) VALUES (?, ?)");
$stmt->execute([1, "Robert'); DROP TABLE users;"]);
// Print "Malicious input stored safely as plain data" to the output
echo "Malicious input stored safely as plain data";
?>
Login to try C/C++/Java/PHP code in the editor
- Claiming an interface can hold method bodies or instance properties like an abstract class, when an interface only declares method signatures (and constants) and each class must implement them.
- Storing sensitive data such as a user's role or login state directly in a cookie, when the user can read and edit cookies; keep the data in
$_SESSIONon the server and store only the session ID in the cookie. - Building SQL by concatenating user input, like
"... WHERE id = $id", instead of using PDO prepared statements with placeholders, which leaves the query open to SQL injection.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: