PHP Super Global variables, magic variables and Magic methods

Source: Internet
Author: User
Tags set cookie setcookie
PHP has been designed with 9 super-global variables, 8 magic variables, and 13 magic methods, which can be used anywhere in the script without a declaration. These variables and functions are frequently used in PHP development, and these variables and functions can easily help us solve many problems. The following is a detailed explanation of the hyper-global variables, magic variables, and Magic methods in PHP.

PHP Super Global Variables (9)

$GLOBALS store variables in the global scope

$_server Getting server-related information

$_request getting the parameters for post and get requests

$_post get the form's POST request parameters

$_get GET request parameters for a form

$_files get the variable to upload the file

$_env getting an array of server-side environment variables

$_cookie Browser COOKIE operation

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

Get cookie:$_cookie["user"];

Delete Cookie:setcookie ("User", "", Time ()-3600);//Set Expiration

Operation of $_session server SESSION

Be sure to Session_Start () Start session before using the session

Storage session:$_session["name"]= "King";//Array operation

Destroy Session:unset ($_session["name"]);//Destroy A

Session_destroy () and unset ($_session);//Destroy All SESSION

PHP Magic variables (8)

The current line number in the __line__ file.

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

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

The __FUNCTION__ constant returns the name of the function when it is defined

The __CLASS__ constant returns the name of the class when it is defined (case-sensitive).

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

The method name of the __method__ 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 is new).

PHP Magic Method (13)

__construct () is called when an object is instantiated, and when __construct and a function with a class name are present, __construct is called and the other is not called.

__destruct () is called when an object is deleted or when an object operation terminates.

The __call () object invokes a method that is called directly if the method exists, or calls the __call function if it does not exist.

__get () When the property of an object is read, the property value is returned directly if it exists, and the __get function is called if it does not exist.

__set () Sets the property of an object, assigns the value directly if the attribute exists, or calls the __set function if it does not exist.

__tostring () is called when an object is printed. such as Echo $obj; or print $obj;

__clone () is called when the object is cloned. such as: $t =new Test (); $t 1=clone $t;

__sleep () serialize was called before. If the object is relatively large, want to cut a bit of the east and then serialize, you can consider this function.

__wakeup () Unserialize is called to do some initialization of the object.

__isset () is called when detecting whether an object's properties exist. such as: Isset ($c->name).

__unset () is called when a property of an object is unset. such as: unset ($c->name).

Called when the __set_state () call is Var_export. Use the return value of __set_state as the return value of Var_export.

__autoload () When an object is instantiated, the method is called if the corresponding class does not exist.

  • Related Article

    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.