Three ways to use PHP namespace

Source: Internet
Author: User
PHP 5.3 In namespace is actually a good thing, you can simplify the programming, the following describes the three categories in the code
Methods for accessing classes in namespace
1 referencing namespace and classes
Suppose the namespace program is namespaced-class.php

namespace Christmas\daysof;  Class partridgeinapeartree{}

Reference method:

  Include ' namespaced-class.php '; $bird 1 = new Christmas\daysof\partridgeinapeartree (); Var_dump ($bird 1);

At this time, new has introduced the complete namespace and the following classes.
2 Partial references

  Include ' namespaced-class.php '; use christmas\daysof; $bird 2 = new Daysof\partridgeinapeartree (); Var_dump ($bird 2);

With the use specified in the namespace, it is only necessary to refer to the last part of the namespace when using Daysof.
3 The simplest

Include ' namespaced-class.php '; use Christmas\daysof\partridgeinapeartree as Bird;            $bird 3 = new Bird (); Var_dump ($bird 3);

In this case, the specified class under the namespace is replaced by a custom name, which is very convenient.
Related recommendations:

Using the PHP page static steps

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.