← Back to PHP Course | Chapter 15: Web Development | Lesson 6 of 12

PHP Interview Questions

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
<?php
interface Shape {
    function area();
}
abstract class Vehicle {
    function honk() { echo "Beep\n"; }
    abstract function drive();
}
echo "Interfaces: contract only. Abstract classes: can mix concrete + abstract methods.";
?>

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
<?php
interface Logger {
    function log($msg);
}
class FileLogger implements Logger {
    function log($msg) { echo "File log: $msg"; }
}
class App {
    private $logger;
    function __construct(Logger $logger) {
        $this->logger = $logger;
    }
    function run() { $this->logger->log("App started"); }
}
(new App(new FileLogger()))->run();
?>

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
<?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.";
?>

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
<?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";
?>

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
<?php
$pdo = new PDO('sqlite::memory:');
$pdo->exec("CREATE TABLE users (id INTEGER, name TEXT)");
$stmt = $pdo->prepare("INSERT INTO users (id, name) VALUES (?, ?)");
$stmt->execute([1, "Robert'); DROP TABLE users;"]);
echo "Malicious input stored safely as plain data";
?>

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.