1. <tfoot id='5yPHt'></tfoot>

    <i id='5yPHt'><tr id='5yPHt'><dt id='5yPHt'><q id='5yPHt'><span id='5yPHt'><b id='5yPHt'><form id='5yPHt'><ins id='5yPHt'></ins><ul id='5yPHt'></ul><sub id='5yPHt'></sub></form><legend id='5yPHt'></legend><bdo id='5yPHt'><pre id='5yPHt'><center id='5yPHt'></center></pre></bdo></b><th id='5yPHt'></th></span></q></dt></tr></i><div id='5yPHt'><tfoot id='5yPHt'></tfoot><dl id='5yPHt'><fieldset id='5yPHt'></fieldset></dl></div>

      <small id='5yPHt'></small><noframes id='5yPHt'>

      • <bdo id='5yPHt'></bdo><ul id='5yPHt'></ul>
      <legend id='5yPHt'><style id='5yPHt'><dir id='5yPHt'><q id='5yPHt'></q></dir></style></legend>

      什么是命名空间?

      What are namespaces?(什么是命名空间?)
      <i id='vdCok'><tr id='vdCok'><dt id='vdCok'><q id='vdCok'><span id='vdCok'><b id='vdCok'><form id='vdCok'><ins id='vdCok'></ins><ul id='vdCok'></ul><sub id='vdCok'></sub></form><legend id='vdCok'></legend><bdo id='vdCok'><pre id='vdCok'><center id='vdCok'></center></pre></bdo></b><th id='vdCok'></th></span></q></dt></tr></i><div id='vdCok'><tfoot id='vdCok'></tfoot><dl id='vdCok'><fieldset id='vdCok'></fieldset></dl></div>
        <tbody id='vdCok'></tbody>

              <bdo id='vdCok'></bdo><ul id='vdCok'></ul>

              <tfoot id='vdCok'></tfoot>
              • <legend id='vdCok'><style id='vdCok'><dir id='vdCok'><q id='vdCok'></q></dir></style></legend>

                <small id='vdCok'></small><noframes id='vdCok'>

                本文介绍了什么是命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                什么是 PHP 命名空间?

                What are PHP Namespaces?

                一般来说什么是命名空间?

                What are Namespaces in general?

                带有示例的外行回答会很棒.

                A Layman answer with an example would be great.

                推荐答案

                命名空间对函数和类的作用就像作用域对变量的作用一样.它允许您在同一程序的不同部分使用相同的函数或类名,而不会导致名称冲突.

                Namespacing does for functions and classes what scope does for variables. It allows you to use the same function or class name in different parts of the same program without causing a name collision.

                简单来说,将命名空间视为一个人的姓氏.如果有两个名为John"的人,您可以使用他们的姓氏来区分他们.

                In simple terms, think of a namespace as a person's surname. If there are two people named "John" you can use their surnames to tell them apart.

                假设您编写的应用程序使用名为 output() 的函数.您的 output() 函数获取页面上的所有 HTML 代码并将其发送给用户.

                Suppose you write an application that uses a function named output(). Your output() function takes all of the HTML code on your page and sends it to the user.

                稍后您的应用程序变得更大并且您想要添加新功能.您添加一个允许您生成 RSS 提要的库.该库还使用名为 output() 的函数来输出最终提要.

                Later on your application gets bigger and you want to add new features. You add a library that allows you to generate RSS feeds. This library also uses a function named output() to output the final feed.

                当你调用 output() 时,PHP 如何知道是使用你的 output() 函数还是 RSS 库的 output()功能?它没有.除非你正在使用命名空间.

                When you call output(), how does PHP know whether to use your output() function or the RSS library's output() function? It doesn't. Unless you're using namespaces.

                我们如何解决拥有两个 output() 函数的问题?简单的.我们将每个 output() 函数固定在其自己的 命名空间.

                How do we solve having two output() functions? Simple. We stick each output() function in its own namespace.

                看起来像这样:

                namespace MyProject;
                
                function output() {
                    # Output HTML page
                    echo 'HTML!';
                }
                
                namespace RSSLibrary;
                
                function output(){
                    # Output RSS feed
                    echo 'RSS!';
                }
                

                稍后当我们想要使用不同的功能时,我们会使用:

                Later when we want to use the different functions, we'd use:

                MyProjectoutput();
                RSSLibraryoutput();
                

                或者我们可以声明我们在其中一个命名空间中,然后我们可以调用该命名空间的 output():

                Or we can declare that we're in one of the namespaces and then we can just call that namespace's output():

                namespace MyProject;
                
                output(); # Output HTML page
                RSSLibraryoutput();
                

                没有命名空间?

                如果我们没有命名空间,我们必须(可能)在添加库时更改大量代码,或者想出繁琐的前缀来使我们的函数名称独一无二.使用命名空间,我们可以避免在将第三方代码与我们自己的项目混合时出现命名冲突的问题.

                No Namespaces?

                If we didn't have namespaces we'd have to (potentially) change a lot of code any time we added a library, or come up with tedious prefixes to make our function names unique. With namespaces, we can avoid the headache of naming collisions when mixing third-party code with our own projects.

                这篇关于什么是命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How do I parse XML containing custom namespaces using SimpleXML?(如何使用 SimpleXML 解析包含自定义命名空间的 XML?)
                SimpleXML SOAP response Namespace issues(SimpleXML SOAP 响应命名空间问题)
                Problems with PHP namespaces and built-in classes, how to fix?(PHP 命名空间和内置类的问题,如何解决?)
                Use php namespace inside function(在函数内部使用 php 命名空间)
                unexpected #39;use#39; (T_USE) when trying to use composer(尝试使用作曲家时意外的“使用(T_USE))
                PHP adding custom namespace using autoloader from composer(PHP使用来自作曲家的自动加载器添加自定义命名空间)
                    <tbody id='pG6vz'></tbody>
                    <legend id='pG6vz'><style id='pG6vz'><dir id='pG6vz'><q id='pG6vz'></q></dir></style></legend>

                    <i id='pG6vz'><tr id='pG6vz'><dt id='pG6vz'><q id='pG6vz'><span id='pG6vz'><b id='pG6vz'><form id='pG6vz'><ins id='pG6vz'></ins><ul id='pG6vz'></ul><sub id='pG6vz'></sub></form><legend id='pG6vz'></legend><bdo id='pG6vz'><pre id='pG6vz'><center id='pG6vz'></center></pre></bdo></b><th id='pG6vz'></th></span></q></dt></tr></i><div id='pG6vz'><tfoot id='pG6vz'></tfoot><dl id='pG6vz'><fieldset id='pG6vz'></fieldset></dl></div>

                    <tfoot id='pG6vz'></tfoot>

                        <small id='pG6vz'></small><noframes id='pG6vz'>

                          <bdo id='pG6vz'></bdo><ul id='pG6vz'></ul>