php editor Strawberry will give you an in-depth understanding of the use keyword in PHP. The use keyword plays an important role in PHP. It is mainly used to import namespaces to make the code more concise and readable. Through the use keyword, classes, functions or constants from other namespaces can be introduced into the current scope, making it easier for developers to call them directly in the code and improving the maintainability and readability of the code. In the process of learning and using PHP, mastering the usage of the use keyword is crucial to improving programming efficiency and code quality.
php
A namespace in PHP is a tag that contains a block of code. We can use namespaces to access specific blocks of code from elsewhere in the project.
For example, a namespace can contain blocks of code such as classes, functions, constants, etc.
Namespaces mainly solve two problems. all these are:
Let's look at an example of how namespaces work. Create a class Greetings
and write a constructor in it.
Display the message Hello everyone!
inside the constructor. Save the file as greetings.php
.
Next, create another file in the same directory as index.php
. First, require greetings.php
using the require
function.
Then, create a variable $hello
and instantiate the Greetings
class instance as $hello = new Greetings
.
When we serve the index.php
file, it gives an error, Class 'Greetings' not found
. To solve this problem we can use namespaces.
To do this, create the namespace subodh\project
in the greetings.php
file. Next, in the index.php
file, use the namespace subodh\project
before the class Greetings
.
This time, the message Hello everyone!
is displayed. This is how you use namespaces to organize components in your project.
We can similarly use namespaces to organize functions and variables.
<code> <code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">namespace</span> subodh\project; </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">class</span> <span style="color:#00f;font-weight:bold">Greetings</span>{ </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">public</span> <span style="color:#008000;font-weight:bold">funct<strong class="keylink">io</strong>n</span> __construct(){ </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">print</span>(<span style="color:#ba2121">"Hello everyone!"</span>)<span style="color:#666">.</span><span style="color:#ba2121">"<br>"</span>; </span></span><span style="display:flex;"><span> } </span></span><span style="display:flex;"><span>} </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">function</span> <span style="color:#00f">greet</span>(){ </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">print</span>(<span style="color:#ba2121">"<strong class="keylink">Go</strong>od Morning!"</span>)<span style="color:#666">.</span><span style="color:#ba2121">"<br>"</span>; </span></span><span style="display:flex;"><span>} </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">const</span> <span style="color:#800">greeting</span> <span style="color:#666">=</span> <span style="color:#ba2121">"have a nice day"</span><span style="color:#666">.</span><span style="color:#ba2121">"<br>"</span>; </span></span></code></code>
<code> <code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">require</span> <span style="color:#ba2121">'greetings.php'</span>; </span></span><span style="display:flex;"><span><span style="color:#19177c">$hello</span> <span style="color:#666">=</span> <span style="color:#008000;font-weight:bold">new</span> subodh\project\Greetings; </span></span><span style="display:flex;"><span>subodh\project\greet(); </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> subodh\project\greeting; </span></span></code></code>
Output:
<code> <code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>Hello everyone! </span></span><span style="display:flex;"><span>Good Morning! </span></span><span style="display:flex;"><span>have a nice day </span></span></code></code>
use
and namespace
We can use the use
keyword in PHP to import the namespace
in PHP and give it an alias. Therefore, we can replace long namespaces with short aliases.
This improves code readability. We can use aliases to represent namespaces.
First, we will use the use
keyword to create an alias for the namespace of the example code written above.
For example, in the index.php
file, write the use
keyword, and import the namespace written in the greetings.php
file as use subodh\project
.
This means that now we can use project
to access classes, functions and constants as shown in the example below.
<code> <code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">require</span> <span style="color:#ba2121">'greetings.php'</span>; </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">use</span> subodh\project; </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#19177c">$hello</span> <span style="color:#666">=</span> <span style="color:#008000;font-weight:bold">new</span> project\Greetings; </span></span><span style="display:flex;"><span>project\greet(); </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> project\greeting; </span></span></code></code>
We can also create custom aliases as follows.
<code> <code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">require</span> <span style="color:#ba2121">'greetings.php'</span>; </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">use</span> subodh\project <span style="color:#008000;font-weight:bold">as</span> pr; </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#19177c">$hello</span> <span style="color:#666">=</span> <span style="color:#008000;font-weight:bold">new</span> pr\Greetings; </span></span><span style="display:flex;"><span>pr\greet(); </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> pr\greeting; </span></span></code></code>
We can also import classes, functions and constants using the use
keyword. We can write the namespace after the use
keyword of the class.
First, we should append the class name at the end of the namespace. We can then access the class directly.
In case of functions and constants, we should write keywords function
and constant
respectively after use
keyword.
After that, we can write the namespace, names of additional functions and constants. An example is shown below.
<code> <code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">use</span> subodh\project\Greetings; </span></span><span style="display:flex;"><span><span style="color:#19177c">$hello</span> <span style="color:#666">=</span> <span style="color:#008000;font-weight:bold">new</span> Greetings; </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">use</span> <span style="color:#008000;font-weight:bold">function</span> <span style="color:#00f">subodh\project\greet</span>; </span></span><span style="display:flex;"><span>greet(); </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">use</span> <span style="color:#008000;font-weight:bold">const</span> <span style="color:#800">subodh\project\greeting</span>; </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> greeting; </span></span></code></code>
The output of all the above methods is the same.
Output:
<code> <code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>Hello everyone! </span></span><span style="display:flex;"><span>Good Morning! </span></span><span style="display:flex;"><span>have a nice day </span></span></code></code>
use
in PHP
As introduced in PHP7, we can group classes, functions and constants when using the use
keyword.
This feature prevents multiple uses of the use
keyword and makes code cleaner and easier to read.
The number of lines of code is also reduced and reusability is maintained. Let us consider the following vehicle.php
file.
It contains two classes, Car
and Motorcycle
, and constructors. Additionally, we created the namespace subodh\project
.
<code> <code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">namespace</span> subodh\project; </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">class</span> <span style="color:#00f;font-weight:bold">Car</span>{ </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">public</span> <span style="color:#008000;font-weight:bold">function</span> __construct(){ </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">print</span>(<span style="color:#ba2121">"This is Car class"</span>)<span style="color:#666">.</span><span style="color:#ba2121">"<br>"</span>; </span></span><span style="display:flex;"><span> } </span></span><span style="display:flex;"><span>} </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">class</span> <span style="color:#00f;font-weight:bold">Motorcycle</span>{ </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">public</span> <span style="color:#008000;font-weight:bold">function</span> __construct(){ </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">print</span>(<span style="color:#ba2121">"This is Motorcycle class"</span>)<span style="color:#666">.</span><span style="color:#ba2121">"<br>"</span>; </span></span><span style="display:flex;"><span> } </span></span><span style="display:flex;"><span>} </span></span></code></code>
We can import both classes as the same namespace using the use
keyword once. We can include the class name in curly braces after the namespace.
We can even create an alias for this class. Commas separate class names.
例如,在 use
关键字之后写入命名空间 subodh\project\{}
。然后,在花括号内,写上 Car
类,并在逗号后写摩托车
类。
最后,为 Motorcycle
类写别名 bike
。现在,我们可以通过使用 new
关键字实例化 Car
和 bike
来创建这些类的对象。
<code> <code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">require</span>(<span style="color:#ba2121">'vehicle.php'</span>); </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">use</span> subodh\project\{Car, Motorcycle <span style="color:#008000;font-weight:bold">as</span> bike}; </span></span><span style="display:flex;"><span><span style="color:#19177c">$car</span> <span style="color:#666">=</span> <span style="color:#008000;font-weight:bold">new</span> Car; </span></span><span style="display:flex;"><span><span style="color:#19177c">$bike</span> <span style="color:#666">=</span> <span style="color:#008000;font-weight:bold">new</span> bike; </span></span></code></code>
输出:
<code> <code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>This is Car class </span></span><span style="display:flex;"><span>This is Motorcycle class </span></span></code></code>
因此,我们可以使用 use
关键字对 PHP 命名空间中的类进行分组。我们也可以类似地对函数和常量进行分组。
The above is the detailed content of use keyword in PHP. For more information, please follow other related articles on the PHP Chinese website!