PHP Learning Essays

Source: Internet
Author: User

PhpMagic Variable

PHP provides a large number of predefined constants to any script it runs.

However, many constants are defined by different extensions, and are only present when the extensions are loaded, either dynamically after loading, or they are included at compile time.

There are eight magic constants whose values change as they change position in the code.

For example, the value of __line__ depends on the row it is in the script to determine. These special constants are case-insensitive, as follows:

__line__

The current line number in the file.

__file__

The full path and file name of the file. If used in the included file, returns the file name that is included.

From PHP 4.0.2, __file__ always contains an absolute path (if it is a symbolic connection, the resolved absolute path), and the previous version sometimes contains a relative path.

__dir__

The directory where the file resides. If used in the included file, returns the directory where the included files are located.

It is equivalent to DirName (__file__). Unless it is a root directory, the name in the directory does not include the trailing slash. (New in PHP 5.3.0)

__function__

The name of the function (PHP 4.3.0 new addition). From PHP 5 This constant returns the name (case-sensitive) when the function is defined. In PHP 4, this value is always in lowercase letters.

__class__

The name of the class (PHP 4.3.0 new addition). From PHP 5 This constant returns the name of the class when it is defined (case-sensitive).

In PHP 4, this value is always in lowercase letters. The class name includes its declared action area (for example, Foo\bar). Note since PHP 5.4, __CLASS__ has also worked for trait. When used in the trait method, __class__ is the name of the class that invokes the trait method.

__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.

__method__

The method name of the class (PHP 5.0.0 new addition). Returns the name of the method when it is defined (case-sensitive).

__namespace__

The name of the current namespace (case-sensitive). This constant is defined at compile time (PHP 5.3.0 new)

PHP Namespaces (namespace)

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:

  1. User-written code conflicts with the name of a class/function/constant or third-party class/function/constant inside PHP.
  2. Creates an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem), improving the readability of the source code
  3. Define namespaces

    By default, all constants, classes, and function names are placed in the global space, just as they were before PHP supported namespaces.

    The namespace is declared by the keyword namespace. If a file contains a namespace, it must declare the namespace before all other code.

  4. Sub-namespaces

    As with directories and files, the PHP namespace allows you to specify the name of a hierarchical namespace. Therefore, the name of the namespace can be defined in a hierarchical way:

  5. 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.

    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.

    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.

    4. NAMESPACE keywords and __namespace__ constants

      PHP supports two abstract methods of accessing the inner elements of the current namespace, __namespace__ Magic constants and NAMESPACE keywords.

      The value of the constant __namespace__ is a string containing the name of the current namespace. In the global, not included in any namespace code, it contains an empty string.

      __namespace__ example, the generation in the namespace

      Namespaces: Aliases/imports

      The PHP namespace supports two ways to use aliases or imports: Use aliases for class names, or use aliases for namespace names. Note that PHP does not support importing functions or constants.

      In PHP, aliases are implemented using the operator use. Here is an example of using all the possible three ways to import:

    5. Name resolution follows these rules:

      1. Calls to fully qualified names for functions, classes, and constants are parsed at compile time. For example, new \a\b resolves to class a\b.
      2. All unqualified and qualified names (not fully qualified names) are converted at compile time according to the current import rules. For example, if the namespace a\b\c is imported as C, then the call to c\d\e () is converted to a\b\c\d\e ().
      3. Inside the namespace, all qualified names that are not translated according to the import rule are preceded by the current namespace name. For example, if you call c\d\e ()inside the namespace a\b , c\d\e ( ) is converted to a\b\c\d\e () .
      4. Unqualified class names are converted at compile time based on the current import rule (instead of the short import name with the full name). For example, if the namespace a\b\c is imported as C, then new C () is converted to new a\b\c () .
      5. Within a namespace (for example, a\b), a function call to an unqualified name is parsed at run time. For example, the call to function foo () is parsed like this:
        1. Look for a function named A\b\foo () in the current namespace
        2. Try to find and invoke the function foo ()in global space.
      6. Calls to unqualified or qualified name classes (not fully qualified names) within a namespace (for example, a\b) are resolved at run time. Here is the parsing process for calling new C () and new D\e () : new C () :
        1. Finds the a\b\c class in the current namespace.
        2. Attempt to automatically load class a\b\c.
        Parsing of New D\e () :
        1. Precede the class name with the current namespace name into:a\b\d\e, and then look for the class.
        2. Attempt to automatically load class a\b\d\e.
        In order to refer to global classes in the global namespace, you must use the fully qualified name new \c ().
      7. Object-oriented content
        • class ? Defines the abstract characteristics of a thing. The definition of a class contains the form of the data and the manipulation of the data.

        • Object ? is an instance of the class.

        • member variable ? A variable that is defined inside a class. The value of the variable is not visible to the outside, but can be accessed through member functions, which can be called properties of an object after the class is instantiated as an object.

        • member function ? Defines the data that can be used to access an object inside a class.

        • inheritance ? Inheritance is the mechanism by which subclasses automatically share data structures and methods of the parent class, which is a relationship between classes. When defining and implementing a class, it can be done on top of an already existing class, taking the content defined by the existing class as its own content, and adding a number of new content.

        • Parent class ? A class is inherited by another class, which can be called a parent class, or a base class, or a superclass.

        • sub-class ? A class inherits other classes, called subclasses, or derived classes.

        • polymorphic? Polymorphism refers to the same operation or function, the process can be used on multiple types of objects and obtain different results. Different objects, receiving the same message can produce different results, a phenomenon known as polymorphism.

        • overloaded ? Simply put, the function or method has the same name, but the argument list is not the same case, such a different parameter of the same name between the functions or methods, each other called overloaded functions or methods.

        • abstract ? Abstraction is the abstraction of objects that have consistent data structures (properties) and behaviors (operations) into classes. A class is an abstraction that reflects the important nature of the application and ignores other extraneous content. The division of any class is subjective, but it must be related to the specific application.

        • Package ? Encapsulation refers to the binding of the properties of an object in the real world to the behavior and placement within a logical unit.

        • constructor function ? It is used primarily to initialize an object when it is created, to assign an initial value to an object member variable, and to always use the new operator in the statement that creates the object.

        • destructor? The destructor (destructor), in contrast to the constructor, automatically executes the destructor when the object ends its life cycle (for example, the function where the object is already called). Destructors are often used to "clean up" work (for example, to open up a memory space with new when creating objects, and to release them in a destructor before exiting).

          • Interface

            Using interfaces (interface), you can specify which methods a class must implement, but you do not need to define the specifics of these methods.

            Interfaces are defined by the interface keyword, just like defining a standard class, but all of the methods defined in it are empty.

            All methods defined in the interface must be public, which is the attribute of the interface.

            To implement an interface, use the implements operator. All methods defined in the interface must be implemented in the class, or a fatal error will be reported. A class can implement multiple interfaces, separating the names of multiple interfaces with commas.

          • Abstract class

            Any class, if at least one of its methods is declared abstract, then the class must be declared abstract.

            A class that is defined as abstract cannot be instantiated.

            A method that is defined as abstract simply declares its invocation method (parameter) and cannot define its specific function implementation.

            When inheriting an abstract class, the subclass must define all the abstract methods in the parent class, and the access control for these methods must be the same (or looser) as the parent class. For example, if an abstract method is declared as protected, the methods implemented in the subclass should be declared as protected or public, and cannot be defined as private. The method must be called in the same way, i.e. the type and the required number of parameters must be the same. For example, a subclass defines an optional parameter, and the declaration of the parent abstract method does not, and the declaration does not conflict.

          • Static keyword

            Declaring a class property or method as static (static) can be accessed directly without instantiating the class.

            Static properties cannot be accessed through an object that a class has instantiated (but static methods can).

            Because static methods do not need to be called through an object, the pseudo-variable $this is not available in a static method.

            A static property cannot be accessed by an object through the operator.

            From PHP 5.3.0, a variable can be used to dynamically invoke a class. However, the value of the variable cannot be a keyword self,parent or static.

          • Final keyword

            PHP 5 has a new final keyword. If a method in the parent class is declared final, the child class cannot overwrite the method. If a class is declared final, it cannot be inherited.

          • Calling the parent class construction method

            PHP does not automatically invoke the constructor of the parent class in the constructor method of the subclass. To execute the constructor of the parent class, you need to call parent::__construct () in the constructor method of the child class.

PHP Learning Essays (GO)

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.