Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🔴 Advanced  ·  Lesson 21

Namespaces

Namespaces किस समस्या को हल करते हैं

बड़े project में आपकी अपनी User class हो सकती है, और जो library आपने install की उसमें भी User class हो सकती है। PHP में बिल्कुल एक नाम की दो classes नहीं हो सकतीं — "cannot redeclare" error आती है। Namespaces इसे हल करते हैं, code को एक group नाम देकर, surname की तरह, ताकि दोनों User classes साथ रह सकें।

💡 Namespace को अपने code के folder जैसे label की तरह सोचिए: App\Models\User, Vendor\Auth\User से अलग "User" है।

Namespace define करना

इसे file की पहली statement के रूप में declare कीजिए।

<?php
namespace App\Models;

class User {
    public function greet() {
        return "Hello from App\\Models\\User";
    }
}
?>

अब यह User App\Models की है। दूसरे namespace की कोई और User इससे नहीं टकराएगी।

use के साथ इस्तेमाल

class को use से import कीजिए, फिर छोटे नाम से बुलाइए।

<?php
require "User.php";
use App\Models\User;

$u = new User();
echo $u->greet();
?>
Hello from App\Models\User

use बिना, हर बार पूरा path लिखना पड़ता: new \App\Models\User()। use line code छोटा और साफ़ रखती है।

as से aliasing

अगर दो classes का नाम एक हो, तो एक को as से locally rename कीजिए।

<?php
use App\Models\User as AppUser;
use Vendor\Auth\User as AuthUser;

$a = new AppUser();
$b = new AuthUser();
?>

अब दोनों "User" classes एक ही file में अलग local नामों से इस्तेमाल हो सकती हैं — न टकराव, न confusion।

Sub-namespaces

Namespaces backslashes से nested हो सकते हैं ताकि folder structure से मेल खाएँ।

<?php
namespace App\Controllers\Admin;
// पूरा नाम: App\Controllers\Admin\DashboardController
class DashboardController { }
?>

Convention से App\Controllers\Admin, folder app/Controllers/Admin/ से मेल खाता है। यही साफ़ मेल autoloaders को classes अपने-आप ढूँढने देता है।

आम गलतियाँ

  • namespace पहले नहीं। namespace line <?php के बाद पहली statement होनी चाहिए।
  • use भूलना। तब पूरा path लिखना पड़ता है वरना PHP class नहीं ढूँढ पाती।
  • / और \ मिलाना। Namespaces backslashes \ इस्तेमाल करते हैं, forward slashes नहीं।
  • Import के बाद name clash। एक जैसे नाम वाली दो imports को एक rename करने को as चाहिए।

सारांश

  • Namespaces code group करते हैं ताकि एक नाम वाली classes न टकराएँ।
  • पहली statement के रूप में namespace App\Models; declare कीजिए।
  • use class import करता है ताकि छोटा नाम इस्तेमाल हो।
  • as import को rename करके clash बचाता है।
  • Namespaces आमतौर पर folders से मेल खाते हैं और autoloading चलाते हैं।

आगे, namespaced classes अपने-आप load कीजिए PHP autoloading से, या code व्यवस्थित कीजिए MVC pattern से।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

PHP में namespaces किस समस्या को हल करते हैं?
जैसे-जैसे project बड़ा होता है, दो classes का नाम एक हो सकता है — जैसे आपका User और किसी library का User। Namespaces code को group करके इन्हें टकराने से रोकते हैं, हर एक को एक तरह का "surname" (उसका namespace) देकर।
Namespace कैसे define करें?
file की बिल्कुल पहली line (<?php के बाद) में namespace App\Models; रखिए। उस file की हर चीज़ फिर App\Models namespace की हो जाती है।
use keyword क्या करता है?
use किसी namespace से class import करता है ताकि आप उसे पूरे path के बजाय छोटे नाम से बुला सकें। use App\Models\User; से आप लंबे रूप की जगह new User() लिख सकते हैं।
as से aliasing क्या है?
अगर दो import की classes का नाम एक हो, या नाम लंबा हो, तो उसे locally rename कर सकते हैं: use App\Models\User as AppUser;। फिर उस file में AppUser इस्तेमाल कीजिए।
क्या namespaces folders जैसे ही हैं?
बिल्कुल नहीं, पर आमतौर पर convention से मेल खाते हैं। class App\Models\User आम तौर पर app/Models/User.php में होती है। यही मेल autoloading को सुचारू बनाता है।
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicAutoloading Classes आपकी प्रगति: 21 / 50 (42%) · Lesson 22 / 50 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें