In the PHP manual, what does this section about namespaces mean?

Source: Internet
Author: User
Note that you can use a fully qualified name to access any global class, function, or constant, such as strlen (), Exception, or INI_ALL. {Code...} Can extendsException be directly written as extendsException? That is to say, the system functions or classes can be written... note that you can use a fully qualified name to access any global class, function, or constant, such as \ strlen (), \ Exception, or \ INI_ALL.

class APIConnectionException extends \Exception {    public $isResponseTimeout;    function __construct($message, $isResponseTimeout = false) {        parent::__construct($message);        $this->isResponseTimeout = $isResponseTimeout;    }}

Hereextends \ExceptionCan be directly writtenextends Exception? That is to say, the system functions or classes can be written\ExceptionOr\strlen()Can also be writtenExceptionOrstrlen()?
The answer is yes or no. If you want to add it, please add it!

Reply content:

Note that you can use a fully qualified name to access any global class, function, or constant, such as \ strlen (), \ Exception, or \ INI_ALL.

class APIConnectionException extends \Exception {    public $isResponseTimeout;    function __construct($message, $isResponseTimeout = false) {        parent::__construct($message);        $this->isResponseTimeout = $isResponseTimeout;    }}

Hereextends \ExceptionCan be directly writtenextends Exception? That is to say, the system functions or classes can be written\ExceptionOr\strlen()Can also be writtenExceptionOrstrlen()?
The answer is yes or no. If you want to add it, please add it!

In your example, the answer is "yes"

If PHP does not specify a namespace for a class or function, its namespace is the current namespace by default, and the current namespace of this Code is empty, it is the root space.\So you can either write or not, because they all point to a space.

If you declare a namespace before the class

namespace Some\Namespace;

There will be a difference, for example\ExceptionThe previous backslash is removed. According to the preceding principle, the current namespace becomesSome\NamespaceSo it will goSome\Namespace\ExceptionIn the end, an error that you cannot find will be thrown.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.