PHP PHP

Home >Backend Development >PHP Tutorial >Modern-php book excerpt (1) namespace Huawei espace computer version download espace.net patents renault espac

Modern-php book excerpt (1) namespace Huawei espace computer version download espace.net patents renault espac

WBOY
WBOYOriginal
2016-07-29 08:54:291621browse

data-id="1190000004892254">

Namespace

Declaration of namespace

  • The namespace is declared at the top of the PHP file, on the first line after the

  • The namespace declaration statement begins with It starts with namespace, followed by a space, then the name of the namespace, and finally ends with;;

  • The manufacturer namespace, namely "Oreilly" declared below, is the most important namespace; it must be globally unique.

<code><?php
namespace Oreilly;</code>

Subnamespace

<code><?php
namespace Oreilly\ModernPHP;</code>

Ps: All classes, interfaces, and functions under the same namespace do not need to be declared in the same PHP file;
So, we can write the same name in different files Multiple classes of spaces.

import and alias

Before namespace was introduced in PHP, developers used Zend-style class names to solve naming conflicts;

<code># Zend_Cloud_DocumentService_Adapter_WindowsAzure_Query => Zend/Cloud/DocumentService/Adapter/WindowsAzure/Query.php</p>
<p>But as you can see, it’s too long and can’t be tolerated! ! ! <br>namespace provides import and alias to solve this problem. <br>import, alias supports class, interface and namespace import in version 5.3. 5.6 starts to support function and constant import. </p>
<pre class="brush:php;toolbar:false"><code># namespace without alias
<?php
$response = new \Symfony\Component\HttpFoundation\Response('Oops',400);
$response->send();
$response2 = new \Symfony\Component\HttpFoundation\Response('Success',200);</code>
<code># namespace with Default alias 
use Symfony\Component\HttpFoundation\Response;
$response = new Response('Oops',400);
$response->send();</code>
<code># namespace with custom alias 
use Symfony\Component\HttpFoundation\Response as Res;
$response = new Res('Oops',400);
$response->send();</code>

Note:

  • Same as the namespace declaration, use the use keyword at the top of the PHP file, and after the

  • No need to add a symbol at the beginning, because PHP import is complete Qualification;

  • use must appear in the global scope, because use is used at compile time.

PHP5.6 and later can import functions and constants;

<code><?php
use func Namespace\functionName;

functionName();</code>

Import constants:

<code><?php
use constant Namespace\CONST_NAME;

echo CONST_NAME;
</code>

Practical tips

Multiple imports

If you want to import multiple classes, interfaces, functions or constants in a PHP file, you need Use multiple use statements;

Not recommended:

<code><?php
use Symfony\Component\HttpFoundation\Request,
    Symfony\Component\HttpFoundation\Response,
    Symfony\Component\HttpFoundation\Cookie;</code>

Recommended:

<code><?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Cookie;</code>

PHP allows one file to define multiple namespaces [Strongly not recommended]

<code><?php
namespace Foo {
    //...
}

namespace Bar {
    //...
}</code>

Global namespace

<code><?php
namespace My\App;

class Foo
{
    public function doSomething()
    {
        $ex = new \Exception();
    }
}</code>

NOTE: At this time, in the Exception class Adding a prefix before the name tells PHP to search for Exception globally. By default, it will search in the current namespace;

The above has introduced the Modern-php book excerpt (1) namespace, including espace and modern aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn