Several ways to load class files in PHP

Source: Internet
Author: User
Load class files in PHP, commonly used require statements or include statements. The difference is that the Require statement contains a file that, if there is a syntax error or does not exist, prompts the error "fatal error" and terminates the program; The include statement, in the same case, prompts the warning "warning" and the program continues to run.

PHP load class files can also be used require_once or include_once, they are different from the Require statement or include statement: the inclusion of the file will check whether the same file is already included, if any, it will not be repeated. However, the use of require_once or include_once is difficult to avoid, resulting in a decrease in efficiency.

Starting with PHP5, you can implement automatic loading of classes by defining a _autoload () function. This function is called automatically when PHP attempts to use an undefined class.
_autoload () Usage:
./myclass.php
<?phpclass myClass {public    function __construct () {        echo "MyClass init ' ed successfuly!!!";    }}? >

./index.php
<?phpfunction __autoload ($classname) {    $filename = "./". $classname. ". PHP ";    Include_once ($filename);} $obj = new MyClass ();? >


Several ways to load class files in PHP

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.