PHP Namespaces
In this page:
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
Namespaces Declare करना
आप किसी file में सबसे पहले statement की तरह namespace keyword से एक namespace declare करते हैं, जैसे namespace App\Models;, और PHP उस declaration के बाद की हर चीज़ को उस namespace से belong करती हुई मानता है।
उदाहरण: Declaring Namespaces
<?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();
?>
Login to try C/C++/Java/PHP code in the editor
Namespaces इस्तेमाल करना
use keyword current file में एक namespaced class import करता है ताकि आप हर बार पूरा namespaced path लिखने के बजाय इसे इसके short name से refer कर सकें।
उदाहरण: Using Namespaces
<?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();
}
?>
Login to try C/C++/Java/PHP code in the editor
use Keyword
एक leading backslash, जैसे \DateTime, global namespace में एक class को refer करता है, जो तब मायने रखता है जब आप किसी custom namespace के अंदर हों और आपको अपनी same-named class के बजाय explicitly किसी built-in PHP class तक पहुँचना हो।
उदाहरण: The use Keyword
<?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();
?>
Login to try C/C++/Java/PHP code in the editor
Sub-Namespaces
Namespaces वह foundation हैं जिस पर Composer की autoloading depend करती है, namespace paths को folder structures में map करते हुए ताकि third-party packages की classes manual require statements के बिना automatically load हो सकें।
उदाहरण: Sub-Namespaces
<?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();
?>
Login to try C/C++/Java/PHP code in the editor
namespaceको दूसरे code के बाद रखना, जबकि यह file में पहला statement होना ज़रूरी है।- किसी namespace के अंदर
\Exceptionजैसी global classes के लिए leading backslash भूल जाना। usestatement भूल जाना, ताकि PHP current namespace में class ढूँढे।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: