PHP magic functions, magic constants, and predefined constants

Source: Internet
Author: User
Tags sapi
An object is called when it is instantiated. When _ construct and a function with the class name coexist, __construct will be called, and the other will not be called. 1. magic functions (13)

1. _ construct ()
An object is called when it is instantiated. When _ construct and a function with the class name coexist, __construct will be called, and the other will not be called.

2. _ destruct ()
It is called when an object or object operation is terminated.

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

4. _ get ()
When reading an object property, if the property exists, the property value is directly returned. if the property does not exist, the _ get function is called.

5. _ set ()
When setting an object property, if the property exists, the value is assigned directly;
If not, the _ set function is called.

6. _ toString ()
It is called to print an object. Such as echo $ obj; or print $ obj;

7. _ clone ()
Called when cloning an object. For example: $ t = new Test (); $ t1 = clone $ t;

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

9. _ wakeup ()
Unserialize is called to initialize objects.

10. _ isset ()
It is called to check whether an object property exists. For example, isset ($ c-> name ).

11. _ unset ()
Unset an object property is called. For example, unset ($ c-> name ).

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

13. _ autoload ()
When instantiating an object, if the corresponding class does not exist, this method is called. Example1. _ get () is called when an attempt is made to read an attribute that does not exist.

If you try to read an attribute that does not exist in an object, PHP will give an error message. If the _ get method is added to the class, and we can use this function to implement operations similar to reflection in java.

Class Test
{
Public function _ get ($ key)
{
Echo $ key. "nonexistent ";
}
}

$ T = new Test ();
Echo $ t-> name;
The output is: name does not exist.



2. _ set () is called when you try to write a value to an attribute that does not exist.

Class Test
{
Public function _ set ($ key, $ value)
{
Echo 'to'. $ key. "value". $ value;
}
}

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

The output is aninggo, the value of name.

 

3. _ call () this method is called when an object does not exist.

Class Test
{
Public function _ call ($ Key, $ Args)
{
Echo "the {$ Key} method you want to call 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 want to call does not exist. The parameter is Array.
(
[0] => aning
[1] => go
)

 

4. _ toString () is called when an object is printed. this method is similar to the java toString method. This function is called back when the object is printed directly.

Class Test
{
Public function _ toString ()
{
Return "print Test ";
}
}

$ T = new Test ();
Echo $ t;

When echo $ t; is run, $ t->__ toString () is called, and the program will output: Print Test;

5. _ clone () is called when the object is cloned.

Class Test
{
Public function _ clone ()
{
Echo "I have been copied! ";
}
}

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

Program output: I have been copied!

2. magic constants(8)

1. _ LINE __
Returns the current row number in the file.

2. _ FILE __
Returns the complete file path and file name. If it is used in a include file, the include file name is returned. Starting from PHP 4.0.2, __file _ always contains an absolute path, while earlier versions sometimes contain a relative path. 3. _ DIR __
The directory where the file is located. If it is used in included files, the Directory of the included files is returned. It is equivalent to dirname (_ FILE __). Unless it is the root directory, the name of the directory does not include the slash at the end. (Added in PHP 5.3.0)

4. _ FUNCTION __
Returns the function name (new in PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the function is defined ). In PHP 4, the value is always lowercase letters.

5. _ CLASS __
The name of the returned class (new in PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the class is defined ). In PHP 4, the value is always lowercase letters. 6. _ TRAIT __
Trait name (new in PHP 5.4.0 ). Starting from PHP 5.4, this constant returns the name (case sensitive) of the trait definition ). The Trait name includes the declared region (for example, Foo \ Bar ).

7. _ METHOD __
Method name of the returned class (new PHP 5.0.0 ). Returns the name (case sensitive) when the method is defined ). Format: class name: method name 8, _ NAMESPACE __
Name of the current namespace (case sensitive ). This constant is defined during compilation (new in PHP 5.3.0) 3. predefined constants

PHP program version, such as 4.0.2
The operating system name of the PHP interpreter executed by PHP_ OS, such as Windows
PHP_SAPI is used to determine whether the command line or browser is used for execution. if PHP_SAPI = 'cli ', the command line is used for execution.

E_ERROR
E_WARNING recent warning area
E_PARSE potential syntax problems
E_NOTICE is unusual, but not necessarily an error.

PHP_EOL system line break, Windows is (\ r \ n), Linux is (/n), MAC is (\ r), available from PHP 4.3.10 and PHP 5.0.2
DIRECTORY_SEPARATOR system directory delimiter. for Windows, it is a backslash (\), and for Linux, it is a slash (/)
PATH_SEPARATOR multi-path delimiter. for Windows, it is a backslash (;), and for Linux, it is a slash (:)

The maximum value of PHP_INT_MAX INT. the 32-bit platform value is 2147483647, which is available from PHP 4.4.0 and PHP 5.0.5.
PHP_INT_SIZE INT characters long, 32-bit platform value is 4 (4 bytes), available from PHP 4.4.0 and PHP 5.0.5

 

 IV. PHP runtime environment detection function php_sapi_name ()

This function returns a lowercase string describing the PHP and WEB server interfaces.

Returns the lowercase string that describes the API type used by PHP (the Server API, SAPI.
For example, in PHP of CLI, this string will be "cli". in Apache, there may be several different values, depending on the SAPI used.
Possible values are listed below:
Aolserver, apache, apache2filter, apache2handler, caudium, and 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, which seems to be something like CGI. Each server may provide different APIs, but they all provide CGI.
We can understand that CGI is the SAPI that every server should have. Apache has its own SAPI, and IIS also has its own. However, php can work on these different servers because php supports their respective SAPIs.
PHP-CLI: php command line interface, php can work in this mode can also CGI mode. It is a SAPI, which is similar to the function provided by CGI.

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.