What are traits in PHP and why are they used? PHP में traits क्या हैं और क्यों इस्तेमाल होते हैं?
Short Answer
GET and POST are the two primary HTTP methods used to send data from a client (like a web browser) to a server (PHP). GET appends data to the URL string, making it visible and bookmarkable. POST sends data silently in the HTTP request body, making it suitable for sensitive or large amounts of data.
Comparison Table
| Feature | GET | POST |
|---|---|---|
| Visibility | Visible in the URL (e.g., ?name=prepiq) |
Hidden in the HTTP body |
| Security | Low (never use for passwords) | Higher (safe for passwords/tokens) |
| Size Limit | Limited by URL max length (approx. 2048 chars) | Virtually unlimited |
| Idempotency | Idempotent (safe to refresh/bookmark) | Not idempotent (refreshing resubmits data) |
When to use which?
- Use GET for: Search queries, filtering, sorting, or retrieving data where the state of the database is not modified. GET requests are easily cacheable by browsers and CDNs.
- Use POST for: Logging in, submitting forms, uploading files, or performing any action that changes data in the database (Create, Update, Delete).
Traits कई unrelated classes में methods का सेट reuse करने देते हैं, PHP की single-inheritance सीमा हल करते हुए। एक class एक साथ कई traits इस्तेमाल कर सकती है।
trait Loggable {
public function log(string $message): void {
echo '[' . static::class . '] ' . $message;
}
}
trait Timestampable {
public function touch(): void {
echo 'Updated at ' . date('Y-m-d H:i:s');
}
}
class Order {
use Loggable, Timestampable;
}
$order = new Order();
$order->log('Order created');
$order->touch();| फीचर | Trait |
|---|---|
| Multiple use | एक class कई traits use() कर सकती है |
| Instantiation | Traits सीधे instantiate नहीं हो सकते |
| Conflict resolution | दो traits में same method हो तो insteadof और as कीवर्ड इस्तेमाल करें |
इंटरव्यू टिप: बताएं traits horizontal code reuse हल करते हैं (unrelated classes में shared behavior), जबकि inheritance vertical reuse हल करता है (is-a relationship)।
Was this answer clear?