PHP Super global variables, Magic variables, and magic methods

Source: Internet
Author: User
PHP has predefined 9 Super global variables, 8 Magic variables, and 13 magic methods during design. these variables and functions can be used anywhere in the script without being declared. These variables and functions are frequently used in PHP Development. these variables and functions can help us solve many problems easily. The following describes hyperglobal variables, Magic variables, and magic methods in PHP in detail. PHP has predefined 9 Super global variables, 8 Magic variables, and 13 magic methods during design, these variables and functions can be used anywhere in the script without being declared. These variables and functions are frequently used in PHP Development. these variables and functions can help us solve many problems easily. The following describes hyperglobal variables, Magic variables, and magic methods in PHP in detail.

PHP Super global variables (9)

$ GLOBALS stores variables in the global scope

$ _ SERVER: get SERVER-related information

$ _ REQUEST: GET parameters of POST and GET requests

$ _ POST get the form's POST request parameters

$ _ GET request parameters of the form

$ _ FILES: Get the variables of the uploaded FILES

$ _ ENV: get the server-side environment variable array

$ _ COOKIE browser cookie operations

Set cookie: setcookie (name, value, expire, path, domain );

Get cookie: $ _ COOKIE ["user"];

Delete cookie: setcookie ("user", "", time ()-3600); // Set the Expiration time

$ _ SESSION server session operations

Run session_start () to start the session.

Save session: $ _ SESSION ["name"] = "King"; // array operation

Destroy session: unset ($ _ SESSION ["name"]); // destroy

Session_destroy () and unset ($ _ SESSION); // destroy all sessions

PHP magic variables (8)

The current row number in the _ LINE _ file.

The complete path and FILE name of The _ FILE. If it is used in a file to be included, the file name to be included is returned.

The directory where the _ DIR _ file is located. If it is used in included files, the Directory of the included files is returned.

_ FUNCTION _ constant returns the name when the FUNCTION is defined.

The _ CLASS _ constant returns the name (case sensitive) when the CLASS is defined ).

_ TRAIT _ Trait name (new in PHP 5.4.0 ). Since PHP 5.4.0, PHP has implemented a method for code reuse, called traits.

_ METHOD _ class METHOD name (new PHP 5.0.0 ). Returns the name (case sensitive) when the method is defined ).

_ NAMESPACE _ name of the current NAMESPACE (case sensitive ). This constant is defined during compilation (new in PHP 5.3.0 ).

PHP magic methods (13)

_ Construct () is called when an object is instantiated. When _ construct and a function with the same class name exist at the same time, __construct is called, and the other is not called.

_ Destruct () is called when an object or object operation is terminated.

The _ call () object calls a method. if a method exists, it is called directly. if the method does not exist, the _ call function is called.

When _ get () reads an object's attribute, if the attribute exists, the property value is directly returned. if it does not exist, the _ get function is called.

When _ set () is used to set an object's property, if the property exists, the value is assigned directly. if the property does not exist, the _ set function is called.

_ ToString () is called when an object is printed. Such as echo $ obj; or print $ obj;

_ Clone () is called when an object is cloned. For example: $ t = new Test (); $ t1 = clone $ t;

_ Sleep () serialize is called. If the object is large and you want to delete something and serialize it, consider this function.

_ Wakeup () unserialize is called to initialize the object.

_ Isset () is called to check whether an object's attribute exists. For example, isset ($ c-> name ).

_ Unset () unset the attribute of an object is called. For example, unset ($ c-> name ).

_ Set_state () is called when var_export is called. Use the return value of _ set_state as the return value of var_export.

_ Autoload () is called if the corresponding class does not exist when instantiating an object.

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.