← Back to PHP Course | Chapter 8: OOP | Lesson 12 of 14

PHP Namespaces

एक namespace एक family name जैसा है जो same first name share करने वाले दो लोगों को अलग करता है। यह same नाम वाली दो classes को mix होने से रोकता है।
Syntax
php
namespace Vendor\Package;

class ClassName { }

use Vendor\Package\ClassName;   // import in another file
$object = new ClassName();

Namespaces क्या हैं?

एक namespace related classes, functions, और constants को एक common prefix के तहत group करता है, उन naming collisions को solve करते हुए जो अन्यथा तब होंगी जब दो अलग libraries दोनों Response नाम की एक class define करें।

उदाहरण: What are Namespaces?

php
<?php
// Declare the namespace `App\Models`
namespace App\Models;

// Define the class `Response`
class Response {
    // Define the function `send` with no parameters
    function send() {
        // Print "App\\Models\\Response" to the output
        echo "App\\Models\\Response";
    }
}
// Create a new `Response` instance, stored in `$r`
$r = new Response();
$r->send();
?>

Namespaces Declare करना

आप किसी file में सबसे पहले statement की तरह namespace keyword से एक namespace declare करते हैं, जैसे namespace App\Models;, और PHP उस declaration के बाद की हर चीज़ को उस namespace से belong करती हुई मानता है।

उदाहरण: Declaring Namespaces

php
<?php
// Declare the namespace `App\Models`
namespace App\Models;

// Define the class `User`
class User {
    // Define the constructor `__construct` with no parameters
    function __construct() {
        // Print `"User in namespace: " . __NAMESPACE__` to the output
        echo "User in namespace: " . __NAMESPACE__;
    }
}
new User();
?>

Namespaces इस्तेमाल करना

use keyword current file में एक namespaced class import करता है ताकि आप हर बार पूरा namespaced path लिखने के बजाय इसे इसके short name से refer कर सकें।

उदाहरण: Using Namespaces

php
<?php
namespace App\Models {
    // Define the class `User`
    class User {
        // Define the function `hello` with no parameters
        function hello() { echo "Hello from User"; }
    }
}

namespace App {
    // Import `App\Models\User` into this file
    use App\Models\User;
    // Create a new `User` instance, stored in `$user`
    $user = new User();
    $user->hello();
}
?>

use Keyword

एक leading backslash, जैसे \DateTime, global namespace में एक class को refer करता है, जो तब मायने रखता है जब आप किसी custom namespace के अंदर हों और आपको अपनी same-named class के बजाय explicitly किसी built-in PHP class तक पहुँचना हो।

उदाहरण: The use Keyword

php
<?php
// Declare the namespace `App`
namespace App;

// Define the class `DateTime`
class DateTime {
    // Define the function `today` with no parameters
    function today() {
        // Return `(new \DateTime())->format("Y-m-d")` from this function
        return (new \DateTime())->format("Y-m-d");
    }
}
// Create a new `DateTime` instance, stored in `$d`
$d = new DateTime();
// Print `$d->today()` to the output
echo $d->today();
?>

Sub-Namespaces

Namespaces वह foundation हैं जिस पर Composer की autoloading depend करती है, namespace paths को folder structures में map करते हुए ताकि third-party packages की classes manual require statements के बिना automatically load हो सकें।

उदाहरण: Sub-Namespaces

php
<?php
// Declare the namespace `App\Services\Payment`
namespace App\Services\Payment;

// Define the class `Gateway`
class Gateway {
    // Define the function `charge` with no parameters
    function charge() {
        // Print "Charging via " . __NAMESPACE__ . "\\Gateway" to the output
        echo "Charging via " . __NAMESPACE__ . "\\Gateway";
    }
}
(new Gateway())->charge();
?>
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. namespace को दूसरे code के बाद रखना, जबकि यह file में पहला statement होना ज़रूरी है।
  2. किसी namespace के अंदर \Exception जैसी global classes के लिए leading backslash भूल जाना।
  3. use statement भूल जाना, ताकि PHP current namespace में class ढूँढे।

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.