PHP magic learning _ PHP Tutorial

Source: Internet
Author: User
PHP magic method learning. _ Set_state () var_export can export a set as a string that is executable PHP code. In the object-oriented PHP 5.1.0, a parameter named _ set_stat _ set_state () is introduced ()
Var_export can export a set as a string, which is some executable PHP code. In php5.1.0, a static method named _ set_state is introduced to make var_export support exporting object instances. When you use var_export to export an instance, the exported string contains the code that calls this static method. This method has a parameter, which is an array containing all member attributes of the exported instance. A little abstract. let's look at the example below.
$ V) {$ obj-> $ k = $ v;} return $ obj ;}}$ I = new o; $ I-> age = 21; eval ('$ B = '. var_export ($ I, true ). ';'); // true indicates that var_export returns the exported string instead of printing it out. Print_r ($ B);/* output: stdClass Object ([skill] => php [age] => 21) */?>

_ Clone ()
In php5, the value assignment between objects is always transmitted by address reference. For example, the following example will output 66 instead of 55.
Age = 66; echo $ i2-> age;?>
If you want to pass the information using the actual value, you need to use the clone keyword.
Age = 66; echo $ i2-> age; // output 55?>
However, the instance $ I is cloned here. If a member attribute of $ I is also an instance, this member attribute will still be passed to $ i2 as a reference method. For example:
Sub = new o2; $ i2 = clone $ I; $ I-> sub-> p = 5; echo $ i2-> sub-> p;?>
The final output is 5 rather than 1. That is to say, although $ I and $ i2 do not point to the same instance, their member attribute $ sub points to the same instance. At this time, we must use the _ clone method to copy $ sub. Add the _ clone () method to the o class. As follows:
Sub = clone $ this-> sub; }}// ......?>
In this way, when echo $ i2-> sub-> p;, the value 1 is output.
_ Autoload ()
When you create an instance, if the corresponding class does not exist, __autoload () will be executed. this function has a parameter for the class name corresponding to the instance to be created. In the following example, if/home/surfchen/project/auto. if php exists, the file require. Otherwise, an error of Class test Not Found is printed and the current script is aborted.

Http://www.bkjia.com/PHPjc/371638.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/371638.htmlTechArticle__set_state () var_export can export a collection into a string that is some executable PHP code. In the object-oriented PHP 5.1.0, an object named _ set_stat... is introduced...

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.