PHP Interview Questions
In this page:
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
// 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 का मतलब है कि code को एक specific concrete class के बजाय एक interface या abstraction पर depend करना चाहिए, ताकि एक implementation को दूसरी से swap करने (जैसे database drivers बदलना) के लिए इसे इस्तेमाल करने वाले code को touch न करना पड़े।
उदाहरण: 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 बनाम 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
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 track करता है कि हर value को कितने references point करते हैं और count zero होते ही memory free कर देता है, एक additional cycle-collector के साथ जो एक loop में एक-दूसरे को reference करने वाले objects पकड़ता है जिन्हें अकेले reference counting detect नहीं कर सकता।
उदाहरण: 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
PDO के through prepared statements SQL structure और user-supplied values को database को अलग-अलग भेजते हैं, इसलिए एक malicious input string भी कभी query का हिस्सा नहीं interpret हो सकती -- SQL injection के against सबसे strong defense।
उदाहरण: 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
- यह claim करना कि एक interface method bodies या instance properties एक abstract class जैसे रख सकता है, जबकि एक interface सिर्फ method signatures (और constants) declare करता है और हर class को उन्हें implement करना ज़रूरी है।
- किसी user के role या login state जैसा sensitive data सीधे एक cookie में store करना, जबकि user cookies पढ़ और edit कर सकता है; data को server पर
$_SESSIONमें रखें और सिर्फ session ID को cookie में store करें। - user input concatenate करके SQL बनाना, जैसे
"... WHERE id = $id", placeholders वाले PDO prepared statements इस्तेमाल करने के बजाय, जो query को SQL injection के लिए खुला छोड़ देता है।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: