Exploring the magic methods in PHP

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff to access the constructor. We can instantiate a Device class object like the following code. 1 $ devicenewDevice (iPhone, $ battery); 2. The Destructor :__ destruct () is opposite to the constructor. The Destructor are generated by the system when the object is destroyed.

Welcome to the Linux community forum and interact with 2 million technical staff> with the constructor, We Can instantiate a Device class object like the following code. 1 $ device = new Device ('iphone ', $ battery); 2. the Destructor __destruct () is opposite to the constructor. The Destructor are generated by the system when the object is destroyed.

Welcome to the Linux community forum and interact with 2 million technicians>

With constructors, We Can instantiate a Device class object like the following code.

1 $ device = new Device ('iphone ', $ battery );

2. destructor :__ destruct ()

Contrary to constructors, destructor are automatically called by the system when an object is destroyed. The following describes how to define a constructor in a class:

Public function _ destruct (){

$ This-> disconnect ();

}

In this destructor, The Destructor calls the disconnect () function in the class before the object is destroyed.

Iii. Attribute Overloading

According to the instructions in the PHP manual, the "overload" (overloading) provided by PHP means to dynamically "CREATE" class attributes and methods. We use magic methods.

When you call an undefined or invisible class attribute or method in the current environment, the overload method is called. In other words, when you access a method or attribute that can be accessed in a class, the overload method is not called. All overload methods must be declared as public.

1. _ get ()

When the value of the inaccessible attribute is read, __get () is called.

2. _ set ()

When you assign a value to an inaccessible attribute, __set () is called.

3. _ isset ()

When isset () or empty () is called for an inaccessible attribute, __isset () is called.

4. _ unset ()

When unset () is called for an inaccessible attribute, __unset () is called.

4. convert an object to a string __tostring ()

When we try to treat an object as a normal string, the __tostring method will be called. For example, when we print an object, the function will be automatically called, for example, echo $ object name. If the _ toString method is not defined, PHP will return an error.

V. clone object :__ clone ()

When we clone an object, the magic method _ clone () is automatically called.

Vi. Object serialization

Serialization is a process of converting any data into a string format. Through serialization, we can save a complete object in a file or in a database. One problem with serialization is that not all data can be serialized, such as a database connection.

1. _ sleep ()

When we use the serialize () function for an object, the __sleep () function is called.

2. _ wakeup ()

When we use the unserialize () function for an object, the __wakeup () function is called.

VII. Method Overloading

1. _ call ()

When an inaccessible method is called in an object, __call () is called.

2. _ callStatic () (PHP 5.3)

When an inaccessible method is called in static mode, __callstatic () will be called.

8. Use objects as functions

1. _ invoke

This method is called when we try to use an object as a function.

9. Automatic Loading

1. _ autoload ()

This method allows the program to automatically search for class files and load class files to the current script when instantiating a class, which simplifies the use of require.

[1] [2]

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.