PHP 5: namespaces and exceptions

Source: Internet
Author: User

1 namespace

1.1 Overview

 
<? Phpnamespace my \ name; Class myclass {} function myfunction () {} const myconst = 1; namespace other \ name {//...} $ A = new myclass; $ c = new \ My \ Name \ myclass; $ A = strlen ('Hi'); $ d = namespace \ myconst; $ d = _ namespace __. '\ myconst'; echo constant ($ D);?>

The concepts related to namespaces are shown above. The following describes each point in detail.

The namespace definition is relatively simple. One way is to define the namespace name in the first line of the page. If one page has multiple command spaces, we recommend that you use the C ++ syntax for namespace definition.

Hierarchical namespaces are defined in the path format level1 \ level2 \ level3.

1.2 namespace usage

If the current namespace is A, the names in the current space are searched from, therefore, if the current namespace defines a variable with the same name as the global space, you need to start \ to access the content of the global space. Unless not defined in the current namespace, you do not need to start from.

All things that are not defined in the global namespace exist in the global namespace.

The following describes how to dynamically use a namespace:

 
   

PHP uses the _ namespce _ magic variable and namespace \ To save the string of the current namespace. the string of the global namespace is empty.

1.3 namespace Import

Similar to C ++, PHP uses use to import namespaces. It also supports using the as method to create an alias for a long namespace. To simplify the process, it also supports importing multiple namespaces in one row.

 
<? Phpnamespace Foo; Use my \ full \ classname as another; // The following example is the same as use my \ full \ nsname as nsname; // import a global class use \ arrayobject; $ OBJ = new namespace \ another; // instantiate the foo \ another object $ OBJ = new another; // instantiate the my \ full \ classname object nsname \ subns \ func (); // call the function my \ full \ nsname \ subns \ func $ A = new arrayobject (Array (1 )); // instantiate an arrayobject object // If "use \ arrayobject" is not used, a foo \ arrayobject object is instantiated.?>

1.4 name resolution

PHP has different resolution search policies for different names.

For a class, you only need to find the name of the class in the current namespace. To access the globally named class, you need to declare it from the Root \ in a fully qualified manner.

For functions and constants, if this function or constant does not exist in the current namespace, PHP will return and use the function or constant in the global space.

1.5 name resolution rules

  1. Calls to fully qualified functions, classes, and constants are parsed during compilation. For exampleNew \ A \ BResolved to class
    A \ B.
  2. All unqualified names and qualified names (not fully qualified names) are converted during compilation according to the current import rules. For example, if the namespace
    A \ B \ cImportedC, ThenC \ D \ e ()Will be convertedA \ B \ c \ D \ e ().
  3. Within the namespace, all qualified names that are not converted according to the import rules will be prefixed with the current namespace name. For example
    A \ BInternal callC \ D \ e (), ThenC \ D \ e ()Will be convertedA \ B \ c \ D \ e ().
  4. The unqualified class name is converted during compilation according to the current import rule (the short Import Name is replaced by the full name ). For example, if the namespace
    A \ B \ cIf the data is imported to CNew C ()ConvertedNew A \ B \ c ().
  5. In a namespace (for example, a \ B ), function calls with non-qualified names are parsed at runtime. For example, the
    Foo () function is parsed as follows:

    1. in the current namespace, find the namespace named A \ B \ Foo () Functions
    2. try to find and call global) Functions in the space
      Foo () .
  6. In a namespace (for exampleA \ B) Internal calls to a non-qualified name or qualified name class (non-fully qualified name) are resolved at runtime. The following is the call
    New C ()AndNew d \ e ()Parsing process:New C ()Parsing:

    1. Search in the current namespaceA \ B \ cClass.
    2. Try automatic loading classA \ B \ c.

    New d \ e ()Parsing:

    1. Add the current namespace name before the class name:A \ B \ D \ eAnd then find the class.
    2. Try automatic loading classA \ B \ D \ e.

    To reference global classes in a global namespace, you must use a fully qualified name.New \ c ().

2 exceptions

Java-like exceptions are introduced in PHP, which are similar in usage. If exceptions are not caught,CodeAn exception is not captured. You can defineSet_exception_handler ()To handle these exceptions.

It is best to initialize built-in exceptions for user-defined exceptions.

 
Class myexception extends exception {// redefine the constructor to change the message to the public function _ construct ($ message, $ code = 0) attribute that must be specified) {// custom code // ensure that all variables are correctly assigned parent ::__ construct ($ message, $ Code );} // custom string output style public function _ tostring () {return _ class __. ": [{$ this-> code}]: {$ this-> message} \ n";} public function customfunction () {echo "a custom function for this type of exception \ n ";}}

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.