PHP magic methods and magic variables, built-in methods and built-in Variables

Source: Internet
Author: User
Tags autoload

PHP built-in variable: directory_separator

Directory_separator is a PHP built-in command that returns path delimiters related to the operating system. It returns/on Windows, and regrets/on Linux or Unix-like, it is usually used when defining the path containing files or uploading and storing directories.

PHP
Put all
__
The class method starting with (two underscores) is used as a magic method. So when you define your own class methods, do not use
 
__
Is the prefix.

 

1
,
_ Construct ()

When an object is instantiated, the constructor of this object will be called first;

We know
PhP5
A function with the same object model and class name is a class constructor. If both the constructor and
_ Construc ()
Method,
PhP5
Will be called by default
_ Contruct ()
Instead of calling similar name functions
_ Contruct ()
As the default constructor of the class;

 

2
,
_ Destruct ()

The Destructor is executed when all references to an object are deleted or when the object is explicitly destroyed.

 

3
,
_ Get (string $ name)

It is called when you try to read an attribute that does not exist. If you try to read an attribute that does not exist in an object,
PHP
The error message is displayed. If you add
_ Get
Method, and we can use this function to implement a similar
Java
Reflection operations in.

 

4
,
_ Set (string $ name, mixed $ value)

When an undefined variable is assigned a value, it is called.

 

5
,
_ Call (string $ name, array $ arguments)

When an inaccessible method (such as undefined or invisible) is called,
_ Call ()

 

Will be called.

_ Callstatic (
String $ name, array $ arguments
)

When an inaccessible method (such as undefined or invisible) is called in a static method,
_ Callstatic ()

 

Will be called.

 

6
,
_ Tostring ()

Called when printing an object. This method is similar
Java
Of
Tostring
Method. This function is called back when the object is printed directly.

 

7
,
_ Clone ()

Called when the object is cloned.

 

8
,
_ Sleep ()

Serialize ()
The function checks whether a magic method exists.
 
_ Sleep

.
If yes,
_ Sleep ()
The method will be called first,
 
Then perform the serialization operation. This function can be used to clear an object and return an array containing all the variable names of the object. If this method does not return any content
Null


Serialized, resulting in
 
One
E_notice
Error.
_ Sleep

This method is often used to submit unsubmitted data or perform similar operations. At the same time, if you have some large objects that do not need to be saved, this function is very useful.

 

9
,
_ Wakeup ()

And
_ Sleep ()

On the contrary,
Unserialize ()

Checks whether
_ Wakeup

Method. If yes
 
_ Wakeup

Method to prepare the object data in advance.
 
_ Wakeup

It is often used in deserialization operations, such as re-establishing a database connection or performing other initialization operations.

10
,
_ Isset ()

When an undefined variable is called
Isset ()
 

Or
 
Empty ()

,
_ Isset ()

 

Will be called.

 

11
,
_ Unset ()

Unset
The attribute of an object is called. For example:
Unset ($ C-> name)
.

 

12
,
_ Set_state ()

Call
Var_export
Is called. Use
_ Set_state
As
Var_export
.

 

13
,
_ Autoload ()

When instantiating an object, if the corresponding class does not exist, this method is called. In short, it is the automatic loading of classes.
;
When you try to use
PHP
No organized class
,

It will look for
_ Autoload
Global Functions
.

If this function exists
, PHP
It will be called with a parameter
,
The parameter is the name of the class.

 

14
,
_ Invoke ()

When you try to call an object by calling a function,
_ Invoke

 

The method is automatically called.

 

Magic constant
:

_ Line __

The current row number in the file.
 

_ File __

The complete file path and file name. If it is used in a file to be included, the file name to be included is returned. From
 
PHP 4.0.2

Start,
_ File __

It always contains an absolute path (if it is a symbolic link, it is the absolute path after resolution), and the previous version sometimes contains a relative path.
 

_ 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
 
Dirname (_ file __)
. Unless it is the root directory, the name of the directory does not include the slash at the end. (
PHP 5.3.0
)
 
=

_ Function __

Function Name (
PHP 4.3.0

Add ). From
 
PHP 5

Returns the name (case sensitive) when the function is defined ). In
 
PHP 4
The value is always lowercase letters.
 

_ Class __

Class Name (
PHP 4.3.0

Add ). From
 
PHP 5

Start this constant and return the name (case sensitive) when the class is defined ). In
 
PHP 4

The value is always lowercase letters.
 

_ Method __

Class Method Name (
PHP 5.0.0

Add ). Returns the name (case sensitive) when the method is defined ).
 

_ Namespace __

Name of the current namespace (case sensitive ). This constant is defined during compilation (
PHP 5.3.0

New)



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.