Magic methods and Magic constants in PHP

Source: Internet
Author: User
This article mainly introduces the Magic method and Magic constants in PHP, has a certain reference value, now share to everyone, the need for friends can refer to

Magic method

__construct () construction method

When instantiating an object, add in parentheses a list of arguments (which can be understood as an argument to the constructor method) after the class name.

__destruct () destructor method

and the construction method is a pair, the construction method is automatically called when an object is "born", and the destructor is automatically called by the system when an object "disappears"!

The destructor method is called before the object disappears.

Several cases where the object disappears

1, explicitly use the unset function to destroy a variable

2, after the script is finished, it will be destroyed automatically.

3, change the value of the object variable, will also automatically destroy

The role of the destructor method

The main function is to release the extra resources that the object occupies! And not the object itself!



Attention:

Destructors often do not require additional definitions, but only when you release additional resources (which do not belong to this object resource).

Summarize the characteristics of the construction method and the destructor method:

1, all start with __

2, are automatically called

3, It is only when a particular situation occurs that the appropriate method is called

At the same time to meet the above three characteristics of the method in PHP There are many, known as the "Magic method"!

__clone () automatically invokes the Magic method when cloning an object

is to automatically invoke the Magic method when cloning an Object! Responsible for initializing the cloned new objects!

__set () assigns a value to a property that is not accessible

to assign a value to an inaccessible property: __set ()

once the __set () Magic method is defined, the method is automatically executed when the value of the inaccessible attribute is assigned , and the processing right is given to the user himself!

The Magic method requires two parameters:

One is the property name of the property that is not accessible

One is the value of the Inaccessible property


__get () Gets the value of an inaccessible property

get the value of an inaccessible property: __get ()

once the __get () Magic method is defined, the method is automatically executed when the value of the inaccessible property is obtained , and the processing right is given to the user himself!

The Magic method requires only one parameter, which is the property name of the property!


Attention:

__get and __set Two methods are often in the real project are in pairs appear! And, often, these two methods are used to deal with the object's private properties in bulk!


The above code can also restrict the user to add attributes to ensure that the original internal structure of the property does not change!


__unset () Remove inaccessible Properties

to delete an inaccessible property: __unset ()

In general, you can Delete one of the exposed properties of an object through the unset function:

However, if the property is inaccessible ( private or nonexistent, for example), the __unset method is automatically executed , whether it can be deleted successfully or depends on __unset the internal implementation!

This method also requires only one parameter, which is the name of the currently deleted property!


if __unset inside nothing to do, the default is not deleted (of course, will not error!) )

__isset () to determine if an inaccessible property exists

to determine if an inaccessible property exists: __isset ()

If the property is not an accessible property, the execution of the method is automatically triggered when the property is judged to exist!

Similarly, the method also needs a parameter, the technology currently needs to judge the property name!

__call () when calling an Inaccessible object method (non-static method)

When calling an Inaccessible object method (non-static method), the Magic method is automatically executed!

Thinking:

How many parameters do you need for this magic method at this point?

parameter one: method Name,string type

parameter two:array type, because the number of parameters is not determined, only all the parameters are placed in an array inside

If this method does nothing:

__callstatic () calls a non-accessible class method (static method)

when invoking an inaccessible class method (static method), the Magic method is automatically executed, and when you define this method, you need to precede the method name with the static keyword, because the method should be a static method!

A small case

Design a mathematical class to achieve the following objectives:

Call method F1:

1, if the passed in is 1 parameters, return the value of its square

2, If 2 arguments are passed in, return the sum of their squares

3, How to pass in is 3 parameters, return the sum of its cubic

is to use method overloading to accomplish:

__invoke ()

The Magic method is automatically executed when we call the object as a function (or method)! At present rather than very often!

Recall the anonymous function you learned earlier:

Attention:

The reason why you can use a successful calling function for a $func closure object is because there is a __invoke Magic method inside the closure object.

__tostring ()

When we think of an object as a string to use, it will automatically execute the simulation Method!

And the return value of the method can generally be the result of serializing the object into a string!

in fact, at this point the system automatically triggers the execution of the __tostring method:

Magic Constants

__class__

Represents the current class name!

Note The difference from self :

self refers to the class itself (a structure that includes not just the class name), but __class__ is just a class name (class name is only part of the Class!). )

__method__

Represents the current method name!

Several "Magic constants" for PHP

Name

Description

__line__

The current line number in the file.

__file__

The full path and file name of the file. If used in the included file, returns the file name that is included.

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

__function__

The function name. This constant returns the name (case-sensitive) when the function is defined.

__class__

The name of the class, which returns the name of the class when it is defined (case-sensitive). Note since PHP 5.4, __CLASS__ has also worked for trait. When used in the trait method, __class__ is the name of the class that invokes the trait method.

__trait__

Trait's name. 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).

__method__

The method name of the class (PHP 5.0.0 new addition). Returns the name of the method when it is defined (case-sensitive).

__namespace__

The name of the current namespace (case-sensitive).

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.