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

PHP Interview Questions

Interview questions common questions हैं जो लोग यह देखने के लिए पूछते हैं कि आप PHP समझते हैं या नहीं। इन्हें practice करना उन topics पर एक quiz के लिए rehearse करने जैसा है जो आपने सीखे हैं।

Interface बनाम Abstract Class

Interfaces बिना किसी implementation के सिर्फ method signatures declare करते हैं, implementing classes को अपना behavior देने पर मजबूर करते हुए; abstract classes concrete methods को abstract वालों के साथ mix कर सकती हैं, यही वजह है कि interfaces unrelated classes के लिए suit करते हैं जो एक contract share करती हैं जबकि abstract classes closely related classes के लिए suit करती हैं जो code share करती हैं।

उदाहरण: 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 का मतलब है कि code को एक specific concrete class के बजाय एक interface या abstraction पर depend करना चाहिए, ताकि एक implementation को दूसरी से swap करने (जैसे database drivers बदलना) के लिए इसे इस्तेमाल करने वाले code को touch न करना पड़े।

उदाहरण: 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 बनाम Sessions

Cookies browser में plain text की तरह रहती हैं जिसे user inspect या tamper कर सकता है, जबकि sessions actual data server-side रखते हैं और browser को सिर्फ एक opaque ID सौंपते हैं -- यही वजह है कि sensitive data हमेशा एक session में belong करता है, cookie में नहीं।

उदाहरण: 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 track करता है कि हर value को कितने references point करते हैं और count zero होते ही memory free कर देता है, एक additional cycle-collector के साथ जो एक loop में एक-दूसरे को reference करने वाले objects पकड़ता है जिन्हें अकेले reference counting detect नहीं कर सकता।

उदाहरण: 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

PDO के through prepared statements SQL structure और user-supplied values को database को अलग-अलग भेजते हैं, इसलिए एक malicious input string भी कभी query का हिस्सा नहीं interpret हो सकती -- SQL injection के against सबसे strong defense।

उदाहरण: 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. #}
आम गलतियां
  1. यह claim करना कि एक interface method bodies या instance properties एक abstract class जैसे रख सकता है, जबकि एक interface सिर्फ method signatures (और constants) declare करता है और हर class को उन्हें implement करना ज़रूरी है।
  2. किसी user के role या login state जैसा sensitive data सीधे एक cookie में store करना, जबकि user cookies पढ़ और edit कर सकता है; data को server पर $_SESSION में रखें और सिर्फ session ID को cookie में store करें।
  3. user input concatenate करके SQL बनाना, जैसे "... WHERE id = $id", placeholders वाले PDO prepared statements इस्तेमाल करने के बजाय, जो query को 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.