Example of php auto-Loading Mechanism

Source: Internet
Author: User
Tags autoload eol

Example of php auto-Loading Mechanism

This article mainly introduces the php autoload mechanism example, the file structure is as follows, two ways to achieve automatic loading, you can refer to the following

1. User-Defined Functions

 

2, spl_autoload_register ()

 

The Code is as follows:

Liuyuan @ ebuinfo:/var/www/phpgcs/php_autoload $ ll ./*

-Rw-r -- 1 liuyuan 800 Feb 19 :39./func_autoload.php

-Rw-r -- 1 liuyuan 906 Feb 19 :28./spl_autoload.php

 

./Include:

Total 16

Drwxrwxr-x 2 liuyuan 4096 Feb 19 11: 42 ./

Drwxrwxr-x 3 liuyuan 4096 Feb 19 11: 43 ../

-Rw-r -- 1 liuyuan 142 Feb 19 :42 aClass. php

-Rw-r -- 1 liuyuan 143 Feb 19 :42 bClass. php

 

 

First, let's look at the custom function method:

 

The Code is 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 running result is as follows:

 

The Code is 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

)

 

 

Method 2:

The Code is 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 ('myload', 'autoload '));

 

/**

* The _ autoload method will expire 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 using the following method:

*/

// 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 running result is as follows:

The Code is 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.