Php magic methods notes, the magic constants and superglobals

A, magic method (13)

. 1, __ Construct ()
is called when the object is instantiated, and when __construct class function name to the function name exist, __ Construct is invoked, the other is not invoked.

2, __ destruct ()
when an object is deleted or an object is invoked operation is terminated.

3, __ call ()
object to call a method, if the method exists, then a direct call; if not, it will go __call function call.

4, __ get ()
reading properties of an object, if the attribute exists, simply return the property value; if present, the __get function is called.

5, __ set ()
setting properties of an object, if the attribute exists, then the direct assignment;
if present, the __set function is called.

6, __ toString ()
to print an object is called. The echo $ obj; obj $ or Print;

. 7, __ clone ()
is called when the object is cloned. Such as: T = $ new new the Test (); clone $ $ T = T1;

. 8, __ SLEEP ()
is called before serialize. If the object is relatively large, want to cut that stuff again serialization, you can consider this function.

9, __ wakeup ()
is called when unserialize, object initialization work to do.

10, __ isset ()
is called when an object is detected whether the attribute is present. Such as: isset ($ c-> name) .

11, __ unset ()
is invoked when unset attributes of an object. Such as: unset ($ c-> name) .

12, __ set_state ()
is invoked var_export, is called. The return value as var_export with __set_state return value.

13, __ autoload ()
when an object instance, if a corresponding class does not exist, the method is called.
 
for example
1, __ get () when trying to read a property does not exist when they were called.

If you try to read an object when the property does not exist, PHP will give an error message. If the class is added __get method, and we can use this function to implement various operations reflected in similar java.

the Test class 

    public function the __get (Key $) 
    { 
        . Key $ echo "absent"; 
    } 


$ new new T = the Test (); 
echo $ T-> name;
will output: name does not exist



2, __ set () when trying to write the value of a property does not exist when they were called.

the Test class 

    public function the __set (Key $, $ value) 
    { 
        echo 'on' $ key "value attached" $ value;... 
    } 


$ new new T = the Test (); 
$ T-> name = "aninggo"; 

is output: the value of the name attached aninggo

 

3, __ call () when trying to call an object method does not exist, call the method.

the Test class 

    public function __call ($ Key, $ Args) 
    { 
        echo "you want to call {$ Key} method does not exist you pass the parameters are:.." print_r ($ Args, to true); 
    } 


$ t = the Test new new (); 
$ T-> getName (Aning, Go);

the program will output:
getName method you want to call does not exist. Parameters are: the Array
(
    [0] => Aning
    [. 1] => Go
)

 

4, __ toString () when the print is called when an object This method is similar to java's toString method to print directly to the object when we call this function back.

class Test 

    public function __toString() 
    { 
        return "打印 Test"; 
    } 


$t = new Test();
echo $t; 

Run echo $ t; and they will call $ t -> __ toString (); so that the program will print: Print Test;

5, __ clone () when the object to be cloned, is invoked.

the Test class 
{
    public a __clone function () 
    { 
        echo "! I was copied"; 
    }
}

$ new new T = the Test (); 
$ $ clone T1 = T;

program output: I is duplicated!


Second, the magic constants (8)

1, __ __LINE__
returns the file in the current line number.

2, __ FILE__
returns the full path and filename of the file. If the included file is returned that contains the file name. Since PHP 4.0.2 onwards, __ FILE__ always contains an absolute path, and in this previous versions sometimes contain a relative path.
 
3, __ DIR__
directory files are located. If the included file, directory is where the included file is returned. It is equivalent to dirname (__ FILE__). Unless it is the root directory, or directory name does not include the trailing slash. (PHP 5.3.0 the new)

4, __ FUNCTION__
return function name (PHP 4.3.0 Added). Since this constant returns the name of the function is defined from the 5 PHP (case sensitive). In PHP 4 this value is always lower case letters.

5, __ CLASS__
returns the name of the class (PHP 4.3.0 Added). Since this constant returns the class name is defined as the time from 5 PHP (case sensitive). In PHP 4 this value is always lower case letters.
 
6, __ TRAIT__
Trait name (PHP 5.4.0 Added). Since PHP 5.4 onwards This constant returns trait is defined when the name (case-sensitive). Trait name including the role of the region it is declared (eg Foo \ Bar).

7, __ METHOD__
Returns the name of the class (PHP 5.0.0 Added). When returns the name of the method is defined (case sensitive). Format: Class name :: method name
 
8, __ NAMESPACE__
name of the current namespace (case-sensitive). This constant is defined at compile time (PHP 5.3.0 new)
 
 
Third, the predefined constants

PHP_VERSION PHP version of the program, such as 4.0.2
PHP_OS execute PHP interpreter the name of the operating system, such as Windows
PHP_SAPI is used to determine the browser using the command line or executed, if PHP_SAPI == 'cli' representation is performed at the command line

E_ERROR recent error at
E_WARNING recent warning at
E_PARSE syntax analysis of potential problems at the
E_NOTICE occurs unusual but not necessarily an error at

PHP_EOL newline system, Windows is (\ r \ n), Linux is (/ n), MAC is (\ r), since PHP 4.3.10 and PHP 5.0.2 became available
DIRECTORY_SEPARATOR directory separator system, Windows is the backslash (\), Linux is a slash (/)
between PATH_SEPARATOR multi-path separator, Windows is the backslash line (;), is the Linux slash (:)

PHP_INT_MAX the INT maximum value of 2147483647 32-bit platform, from PHP 4.4.0 and available from PHP 5.0.5
PHP_INT_SIZE INT word length, value of 4 (4-byte) 32-bit platform, from PHP 4.4.0 and available from PHP 5.0.5

 

 Four, PHP php_sapi_name operating environment detection function ()

This function returns a description of the PHP WEB server interface lowercase string.

Description Returns the type of interface (the Server API, SAPI) is used PHP lowercase string.
For example, the string will be under the PHP CLI is "cli", may have several different values under Apache, depending on the specific use of SAPI.
The following lists the possible values:
AOLserver, the 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, and CGI is seemingly a thing. API provided by each server may be different, but they both offer CGI.
        CGI can be understood that each server should have SAPI. apache has its own SAPI, IIS also has its own. But php can work in these different server, because php support their respective SAPI.
PHP-CLI: php command line interface, php can work in this mode can also be CGI mode. It is a SAPI It CGI and provides functionality similar.

Original link: https://blog.csdn.net/freedom453/article/details/39320753 (Thanks for sharing)

Guess you like

Origin www.cnblogs.com/luqiang213917/p/11775502.html