PHP Study Notes (3)

Source: Internet
Author: User

PHP class has many differences with other languages. PHP allows an amazing way to call an instance method in a static method. The keywords self and static are provided for static members of the worker class. Self always refers to the current class, while static may point to the derived class, which is a wonderful place. static members can inherit and overwrite them.

 

PHP also provides some magic methods (including constructor methods and some built-in "interfaces") starting with a double underline. below is the existing PHP magic method:

1. _ construct ($ args) and _ destruct (void) 

Constructor and constructor. Similar to other languages, constructor is called during instantiation and the constructor is called when objects are recycled. The Destructor can be used to automatically Recycle resources after execution, such as disabling resources referenced by pointers.

 

2. mixed _ call(String$name, Array$arguments) And mixed_ CallStatic(String$name, Array$arguments)

If you call a method that does not exist in an object or a static method does not exist in the class, the preceding two methods are called respectively. These two methods allow you to use some special dynamic running mechanisms to make the methods come out of nothing. I personally suggest using as few as possible, which will make the code difficult to maintain and cannot be supported by IDE.

 

3. mixed _ get (string$name) And _ set (string $ name, mixed $ value)

Like _ call, they are executed when the call does not exist. We recommend that you use them less.

 

4. bool _ isset (string$name) And _ unset (string$name)

When isset () or empty () is called for attributes that do not exist in an object, __isset () is called. _ Unset is called when unset is called.

 

5. array _ sleep (void) and _ wakeup (void)

These two methods are called before serialization and after serialization respectively, and can be used to sort the attributes of the serialized object and restore the State after serialization.

 

6. string _ toString (void)

It tells PHP how to convert an object to a string, such as echo new MyClass ();

 

7. mixed _ invoke ()

This method is called when an object variable is called as a function.

 

8. object _ set_state (array$properties)

When var_export () is called for an object, it is called, and the return value replaces the object itself, so that it can organize the data of the exported variable.

 

9. _ clone ()

This method allows us to perform deep copy for attributes while copying objects, similar to the ICloneable interface in. net.

 

 

About automatic loading:

PHP itself does not have an automatic loading mechanism, but the new version provides the spl_autoload_register function to register a function. When a non-existent class is used, the registered function is called, developers need to manually implement the loading mechanism in functions. I have not thoroughly understood the include mechanism. If I still need to check whether a file exists, it may reduce the running efficiency.

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.