Object-oriented eighth day----namespaces

Source: Internet
Author: User
Tags aliases

Namespace namespace php5.3
1.1 Overview of namespaces
The application of this principle to the field of programming is the concept of namespaces.
The problem solved:
1. User-written code conflicts with the name of a class/function/constant or third-party class/function/constant within PHP.

2. Create an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem) and improve the readability of the source code.


1.2 Define Namespaces
Format:
Namespace Space Name
Attention:
①namespace before any output including header
② if previously mentioned system conflict issues take precedence over the class function constants in your own namespace
③ introduces a file that is independent of the name of the namespace, the class function constants that must be connected with the access path and that do not have namespaces default to the root space



1.3 Defining a sub-namespace
Namespace lamp\php
Note: The path can be relative and absolute but cannot be used on a single drive. /

1.4 Defining multiple namespaces in the same file

namespace A;
namespace B;
Or
Namespace a{
}
Namespace b{
}

1.5 using namespaces
Unqualified name demo ();
Qualified name Php\demo ();
Fully qualified name \lamp\php\demo ();

1.6 Namespaces and dynamic language features
Constant function classes
Use a fully qualified name when using dynamic language special new

1.7 NAMESPACE keywords and __namespace__ Magic constants
Namespace defining a Namespace
namespace refers to the current namespace name

__NAMESPACE__ represents the current namespace name

1.8 Using namespaces: Aliases/imports
Use aliases replaced with namespace
Use namespace as alias//use LAMP\PHP\OOP as LP;
The use namespace defaults to the last-level space name as an alias
Use Import
Use space name \ class name;

1.9 Global Space
Default in global root space if no namespace

1.10 Using namespaces: Fallback global functions/constants
Function constants can be
Class is not available for
1.11 Name parsing rules

Object-oriented eighth day----namespaces

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.