PHP Auto load autoload Mechanism sample sharing _php instance

Source: Internet
Author: User
Tags autoload eol

1, custom functions

2,spl_autoload_register ()

Copy Code code as follows:

liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ LL./*
-rw-rw-r--1 Liuyuan Liuyuan Feb 11:39./func_autoload.php
-rw-rw-r--1 Liuyuan Liuyuan 906 Feb. 11:28

./include:
Total 16
Drwxrwxr-x 2 Liuyuan Liuyuan 4096 Feb 19 11:42./
Drwxrwxr-x 3 Liuyuan Liuyuan 4096 Feb 19 11:43. /
-rw-rw-r--1 Liuyuan Liuyuan Feb 11:42 aclass.php
-rw-rw-r--1 Liuyuan Liuyuan 143 Feb 11:42

First look at the Custom Function mode:

Copy Code code as follows:

<?php
Define (' EOL ', (Php_sapi = = ' cli ')? Php_eol: ' </br> ');
Print_r (Get_included_files ());
Echo EOL;
Print Get_include_path ();
Echo EOL;
Set_include_path (Get_include_path (). Path_separator. ' /var/www/ly_php/php_spl/include/');
Set_include_path (DirName (__file__). /include ');
Set_include_path (DirName (__file__). /include/');

function __autoload ($className) {
$filename = './include/'. $className. PHP ';
$filename = './include/'. $className. PHP ';
$filename = '/var/www/ly_php/php_spl/include/'. $className. PHP ';
if (file_exists ($filename)) {
Include_once $filename;
}else{
Exit (' no file ');
}
}

$a = new AClass ();
$b = new Bclass ();
Print_r (Get_included_files ());
?>

The results of the operation are as follows:

Copy Code code as follows:

liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ PHP func_autoload.php
Array
(
[0] =>/var/www/phpgcs/php_autoload/func_autoload.php
)

.:/ Usr/share/php:/usr/share/pear
AClass is loaded
Bclass is loaded
Array
(
[0] =>/var/www/phpgcs/php_autoload/func_autoload.php
[1] =>/var/www/phpgcs/php_autoload/include/aclass.php
[2] =>/var/www/phpgcs/php_autoload/include/bclass.php
)

The second way:

Copy Code code as follows:

<?php
Class myloader{
public static function AutoLoad ($className) {
$filename = './include/'. $className. PHP ';
if (file_exists ($filename)) {
Include_once $filename;
}else{
Exit (' no file ');
}
}
}

Define (' EOL ', (Php_sapi = = ' cli ')? Php_eol: ' <br/> ');

Spl_autoload_register (Array (' Myloader ', ' autoload '));

/**
The *__autoload method fails after spl_autoload_register because the AUTOLOAD_FUNC function pointer has pointed to the Spl_autoload method
* You can add the _autoload method to the Autoload_functions list in the following ways
*/
Spl_autoload_register (' __autoload ');


Error_reporting (E_all^e_notice^e_warning^e_error);
Error_reporting (E_notice | e_warning);

$a = new AClass ();
Print_r (Get_included_files ());
Echo EOL;
$b = new Bclass ();
Echo EOL;
?>

The results of the operation are as follows:

Copy Code code as follows:

liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ PHP spl_autoload.php
AClass is loaded
Array
(
[0] =>/var/www/phpgcs/php_autoload/spl_autoload.php
[1] =>/var/www/phpgcs/php_autoload/include/aclass.php
)

Bclass is loaded

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.