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

PHP Interview Questions

Interview questions are common questions people ask to see whether you understand PHP. Practicing them is like rehearsing for a quiz on the topics you have learned.

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

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
// 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();
?>

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
// 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";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. 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.
  2. 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 $_SESSION on the server and store only the session ID in the cookie.
  3. 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.

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.