First, the Magic function(13)
1, __construct ()
Called when an object is instantiated, and when __construct and a function with the name of the class name are present, __construct is called and the other is not called.
2, __destruct ()
Called when an object is deleted or when an object operation terminates.
3, __call ()
The object calls a method, and if the method exists, it is called directly; if it does not exist, it will call the __call function.
4, __get ()
When a property of an object is read, the property value is returned directly if the property exists, or the __get function is called if it does not exist.
5, __set ()
When a property of an object is set, the value is directly assigned if the attribute exists;
If it does not exist, the __set function is called.
6, __tostring ()
Called when an object is printed. such as Echo $obj; or print $obj;
7, __clone ()
Called when the object is cloned. such as: $t =new Test (); $t 1=clone $t;
8, __sleep ()
Serialize before being called. If the object is relatively large, want to cut a bit of the east and then serialize, you can consider this function.
9, __wakeup ()
Unserialize is called to do some initialization of the object.
10, __isset ()
Called when detecting whether an object's properties exist. such as: Isset ($c->name).
11, __unset ()
Called when a property of an object is unset. such as: unset ($c->name).
12, __set_state ()
Called when the Var_export is called. Use the return value of __set_state as the return value of Var_export.
13, __autoload ()
When an object is instantiated, the method is called if the corresponding class does not exist.
examples Show1, __get () is called when attempting to read a property that does not exist.
If you try to read a property that does not exist for an object, PHP will give you an error message. If you add a __get method to a class, and we can use this function to implement various actions like reflection in Java.
Class Test
{
Public Function __get ($key)
{
Echo $key. "does not exist";
}
}
$t = new Test ();
Echo $t->name;
It will output: name does not exist
2, __set () is called when attempting to write a value to a property that does not exist.
Class Test
{
Public Function __set ($key, $value)
{
echo ' pair '. $key. "Attached value". $value;
}
}
$t = new Test ();
$t->name = "Aninggo";
Will output: The value of the name attached to the Aninggo
3, __call () Call this method when attempting to invoke a method that does not exist for an object.
Class Test
{
Public Function __call ($Key, $Args)
{
echo "The {$Key} method you are calling does not exist. The parameter you passed in is: ". Print_r ($Args, true);
}
}
$t = new Test ();
$t->getname (aning, go);
The program will output:
The GetName method you are calling does not exist. parameter is: Array
(
[0] = aning
[1] = = Go
)
4, __tostring () when printing an object is called, this method is similar to the ToString method of Java, when we directly print the object callback with this function.
Class Test
{
Public Function __tostring ()
{
Return "Print Test";
}
}
$t = new Test ();
Echo $t;
When the Echo $t is run, $t->__tostring () is called, and the program outputs: print Test;
5, __clone () is called when the object is cloned.
Class Test
{
Public Function __clone ()
{
echo "I've been copied!" ";
}
}
$t = new Test ();
$t 1 = Clone $t;
Program output: I've been copied!
second, the magic constant(8)
1, __line__
Returns the current line number in the file.
2, __file__
Returns the full path and file name of the file. If used in the include file, the include filename is returned. Since PHP 4.0.2, __file__ always contains an absolute path, and the previous version sometimes contains a relative path. 3, __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)
4, __function__
Returns 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.
5, __class__
Returns 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. 6, __trait__
Trait's name (PHP 5.4.0 new Plus). From PHP 5.4 This constant returns the name of the trait when it is defined (case-sensitive). The Trait name includes its declared function area (for example, Foo\bar).
7, __method__
Returns the method name of the class (PHP 5.0.0 new). Returns the name of the method when it is defined (case-sensitive). Format: Class Name:: Method name 8, __namespace__
The name of the current namespace (case-sensitive). This constant is defined at compile time (PHP 5.3.0 new)
three, predefined constants
Php_version versions of PHP programs, such as 4.0.2
Php_os the name of the operating system that executes the PHP interpreter, such as Windows
Php_sapi is used to determine whether it is executed using the command line or the browser, if the php_sapi== ' CLI ' indicates that it is executed at the command line
E_error the most recent error
E_warning, the nearest warning point.
E_parse Anatomy Grammar has a potential problem
E_notice is unusual, but not necessarily the wrong place.
Php_eol System line break, Windows is (\ r \ n), Linux is (/n), Mac is (\ r), from PHP 4.3.10 and PHP 5.0.2 Available
Directory_separator system directory delimiter, Windows is backslash (\), Linux is slash (/)
Path_separator inter-Path delimiter, Windows is backslash (;), Linux is slash (:)
Php_int_max INT Maximum, 32-bit platform value 2147483647, from PHP 4.4.0 and PHP 5.0.5 Available
Php_int_size INT Word length, 32-bit platform value is 4 (4 bytes), since PHP 4.4.0 and PHP 5.0.5 Available
iv. PHP Run Environment detection function php_sapi_name ()
This function returns a lowercase string that describes the interface between PHP and the Web server.
Returns a lowercase string that describes the type of interface used by PHP (the Server API, SAPI).
For example, the CLI PHP under this string will be "CLI", Apache may have a few different values, depending on the specific use of SAPI.
The possible values are listed below:
Aolserver, Apache, Apache2filter, Apache2handler, Caudium, CGI (until PHP 5.3), cgi-fcgi, CLI, continuity, embed, ISAPI, Litespeed, Milter, Nsapi, phttpd, Pi3web, Roxen, thttpd, Tux and Webjames.
SAPI: Server-side API, seemingly and CGI is a thing. Each server may provide different APIs, but they all provide CGI.
So you can understand that CGI is the SAPI that every server should have. Apache has its own sapi,iis and has its own. But PHP can work on these different servers because PHP supports their respective SAPI.
php-cli:php command line interface, PHP can work in this mode can also be CGI mode. is a kind of sapi, it is similar to the function provided by CGI.
PHP Magic Functions, Magic constants, pre-defined constants