Introduction to the use of php magic functions and magic constants, and php magic function Constants

Source: Internet
Author: User

Introduction to the use of php magic functions and magic constants, and php magic function Constants

Php magic Functions

1. _ Construct ()

Called when instantiating an object,
When _ construct and a function with the class name both exist, __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 ()
Object calls a method,
If a method exists, it is called directly;
If not, the _ call function is called.

4. _ Get ()
When reading the attributes of an object,
If a property exists, the property value is directly returned;
If not, the _ get function is called.

5. _ Set ()
When setting attributes of an object,
If an attribute 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.

Php magic constant

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. _ 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.

4. _ 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.

5. _ METHOD __
Method Name of the returned class (New PHP 5.0.0 ). Returns the name (case sensitive) when the method is defined ).

(1) first knowledge of magic methods
Since the release of Php5.0, many object-oriented features have been provided for us, especially a lot of easy-to-use magic methods. These magic methods allow us to simplify our coding, better design our system. Today, let's get to know the magic methods provided by php5.0.

Comparison between using magic functions in php and without magic Functions

/*** use magic 0.31868386268616s* not use magic 0.11876797676086s*/class Test {  private $varstr = "123";  function __get($name) {    return $this->varstr;  }}function microtime_float(){  list($usec, $sec) = explode(" ", microtime());  return ((float)$usec + (float)$sec);}$time_start = microtime_float();$i = 0;while ($i<100000) {  $i++;  $t = new Test();  $t->varstr;}$time_end = microtime_float();$time = $time_end - $time_start;echo "Did nothing in $time seconds\n";

Try to use less PHP magic Functions

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.