PHP Knowledge Point Summary 3

Source: Internet
Author: User

PHP functions

The real power of PHP comes from its functions.

In PHP, more than 1000 built-in functions are available.


 
 
    • TRAIT Trait's name (PHP 5.4.0 new Plus). Since PHP 5.4.0, PHP has implemented a method of code reuse, called traits.

The Trait name includes its declared function area (for example, Foo\bar).

Members inherited from the base class are overwritten by the Myhelloworld method in the inserted Sayworld Trait. Its behavior is consistent with the methods defined in the Myhelloworld class. The precedence is that methods in the current class override the Trait method, and the trait method overrides the method in the base class.

<?phpclass Base {public    function SayHello () {        echo ' Hello ';    }} Trait Sayworld {public    function SayHello () {        Parent::sayhello ();        Echo ' world! ';    }} Class Myhelloworld extends Base {use    Sayworld;} $o = new Myhelloworld (); $o->sayhello ();? >
    • Method Name (PHP 5.0.0 new). Returns the name of the method when it is defined (case-sensitive)
<?phpfunction Test () {echo  ' function is named: '. __method__;} Test ();? >
    • NAMESPACE The name of the current namespace (case sensitive). This constant is defined at compile time (PHP 5.3.0 is new).
<?phpnamespace Myproject;echo ' namespace: ' ', __namespace__, ' '; Output "MyProject"?>
Name space

PHP namespaces (namespace) are added in PHP 5.3, and if you learn C # and Java, that namespace is nothing new. However, in PHP there is a very important meaning.

The PHP namespace resolves the following two types of problems:

    • The user code conflicts with the name of the class/function/constant inside PHP.
    • Create aliases (or short) names to improve the readability of your source code.

By default, all constants, classes, and function names are placed in the global space, just as they were before PHP supported namespaces.
If a file contains a namespace, it must declare the namespace before all other code

< PHP  //Definition code namespace MyProject in the ' MyProject ' namespace  ;   // ... Code ...  You can also define different namespace codes in the same file namespace MyProject1;  PHP code in the MyProject1 namespace   namespace MyProject2;  PHP code in the MyProject2 namespace     //Another syntax namespace MyProject3 {   //MyProject3 PHP code in namespace    }  ?>

The only valid code before declaring the namespace is the Declare statement that defines how the source file is encoded. All non-PHP code, including whitespace characters, cannot appear before the declaration of the namespace.

<?phpdeclare (encoding= ' UTF-8 '); Define multiple namespaces and code that is not included in the namespace namespace MyProject {Const CONNECT_OK = 1;class Connection {/* ... */}function CONNECT () {/*. . *  /}}namespace {//Global code session_start (); $a = Myproject\connect (); Echo Myproject\connection::start ();}? >

The following code will cause a syntax error:

Sub-namespaces

<?phpnamespace Myproject\sub\level;  Declare a hierarchical single namespace//create a constant myproject\sub\level\connect_ok,//class myproject\sub\level\connection//And Functions Myproject\sub\level\ Connect. Const CONNECT_OK = 1;class Connection {/* ... */}function CONNECT () {/* ... */  }?>

namespaces Use
Class names in the PHP namespace can be referenced in three ways:

      1. Unqualified name, or class name that does not contain a prefix, such as $a =new foo (); or Foo::staticmethod ();. If the current namespace is Currentnamespace,foo, it will be resolved to Currentnamespace\foo. If the code that uses Foo is global and does not contain code in any namespace, Foo is parsed as Foo. Warning: If a function or constant in a namespace is undefined, the unqualified function name or constant name is resolved to the global function name or constant name. ~ ~ No namespace prefix reference, default in the current namespace lookup, and then to the global namespace to find

      2. A qualified name, or a name that contains a prefix, such as $a = new Subnamespace\foo (); or Subnamespace\foo::staticmethod ();. If the current namespace is Currentnamespace, Foo will be parsed to Currentnamespace\subnamespace\foo. If the code that uses Foo is global and does not contain code in any namespace, Foo is parsed as Subnamespace\foo. ~ ~ A (similar relative path) reference with a relative namespace prefix that automatically adds the namespace at the beginning of the calling code

      3. The fully qualified name, or contains the name of the global prefix operator, for example, $a = new \currentnamespace\foo (); or \currentnamespace\foo::staticmethod ();. In this case, Foo is always parsed into the literal name (literal name) Currentnamespace\foo in the code. ~ ~ Reference path with an absolute namespace prefix (similar to absolute path) is explicitly not found

PHP Knowledge Point Summary 3

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.