PHP--Magic method, magic constant Simple Introduction

Source: Internet
Author: User

Magic Method:

PHP treats all the methods in the class that begin with __ (two underscores) as a magic method, and it is generally recommended that users do not prefix the custom method with __. Magic Method:

1. __construct ()

The default constructor method for a class, if __construct () and a method with the same name as the class, is called by default __construct () instead of the same name method. In general, the user-defined construction method will also use __construct ().

2. __destruct ()

A destructor for a class that executes when all references to the object are deleted, or when an object is explicitly destroyed.

3. __get ($name)

Can be summed up as: When reading an object's properties in a $object->a way, if property a exists and is public, the value of the property is returned directly, and if the property A does not exist or is a type that is not directly accessible by protected/private, it will be called __ The Get ($name) method, whichever is the return value. You can generally use this method to allow externally restricted access to internal properties, or to perform reflection operations similar to those in Java.

4. __set ($name, $value)

Similar to __get ($name), when assigning a value to a property in the form of $object->a = 17, if property a exists and is public, then the property A is assigned directly to it, and if the property A does not exist or is a protected/private type, it will be called __ Set ($name, $value) method.

5. __call ($name, $arguments)/ __callstatic ($name, $arguments)

The __call ($name, $arguments) method is called when a method that does not exist or is not accessible is called. The __callstatic ($name, $arguments) method is called when a non-existent or inaccessible method is called in a static method.

6. __tostring ()

It is called directly when the object is printed. such as Echo $object;

7. __clone ()

Called directly when the object is copied. such as $ A = new Action (); $a = $object;

8. __isset ($name)/__unset ($name)

When you use Isset () or empty () for a property that does not exist or is not accessible, __isset () is called, and __unset () is called when unset a nonexistent or inaccessible property, otherwise the attribute is directly unset.

9. __set_state ()

When an object is output with Var_export (), __set_state () is called, and the output is based on the return value of the Magic method. Note: Var_export () is similar to Var_dump (), except that the contents of the Var_export () output conform to the PHP syntax. Note how to use:

$test=NewTest ();$b=Var_export($test,true);Var_dump($b);classTest { Public $a;  Public Static function__set_state ($array) {        $ab=NewTest (); $ab->a = 10; return $ab; }}


__autoload ($name)

When an object is instantiated, the method is called if the corresponding class does not exist. Note: The method is a global function, and the parameter is the name of the class.

__sleep ()/__wakup ()

Slightly.



Magic constants:

1. __line__

Returns the current line number in the file.

<span style="font-size:18px; "><? "". __line__. " Line "  "<br/>";? ></span>



2. __file__

Returns the full path of the file in which it resides. Include file name

<span style="font-size:18px; "><?  "" <br/> ";?" ></span>



3. __function__

Returns the name of the function being located.

<span style="font-size:18px; "><? phpfunction Test () {echo  " This function is named:""<br/> ";} Test ();? ></span>



4. __class__

Returns the name of the class in which it is located.

<span style="font-size:18px;"><?PHPclassTest {function Show () {echo"the class name is:". __class__."<br>"; Echo"the function is named:". __function__; Echo"<br/>";}} $t=Newtest (); $t-Show ();?></span>



5. __method__

Returns the name of the class method in which it is located. Note that __method__ returns the form of "class::function", while __function__ returns the form of "function".

6.__dir__

Returns 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. Does not contain a file name. (New in PHP 5.3.0) =

<span style="font-size:18px; "><? "". __dir__. " folder under "  "<br/>";? ></span>

7.__namespace__

The name of the current namespace (case-sensitive). This constant is defined at compile time (PHP 5.3.0 is new).

8.__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).

PHP--Magic method, magic constant Simple introduction

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.