For PHP loading operations and other two magic methods, load magic

Source: Internet
Author: User

For PHP loading operations and other two magic methods, load magic

<? Php

Load class
// Include ("./Ren. class. php ");
// Include "./Ren. class. php ";
Include_once ("./Ren. class. php ");
Include_once ("./Ren. class. php ");
$ F = new Ren ();
$ F-> test ();

Require ("./Ren. class. php ");
Require_once ("./Ren. class. php ");
Require_once "./Ren. class. php ";
$ F = new Ren ();
$ F-> test ();

Automatic loading class
// 1. All class names and class names must be consistent
// 2. Put all class files under the same file
// 3. The naming rules of all class files are consistent
Function _ autoload ($ cname ){
Require_once ("./$ cname. class. php ");
}
$ T = new test ();
$ T-> ceshi (). '<br> ';
$ S = new Ren ();
$ S-> test ();


Two magic methods
Class Ren {
Public $ name;
Public function say (){
Echo "output object method ";
}
// Method of the output object
Public function _ tostring (){
Echo "Another output method"; // echo $ s->__ tostring ();
Return "Another output method"; // echo $ s;
}
// Method for object cloning
Public function _ clone (){
$ This-> name = "Riven"; // $ this indicates a duplicate (cloned object)
}
}
$ S = new Ren ();
// Echo $ s->__ tostring (); // output string
// $ S-> say ();

$ S-> name = "Small V ";
Var_dump ($ s );

$ S1 = clone $ s;
Var_dump ($ s1 );
?>

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.