SPL Learning Note (5) Use of---functions

Source: Internet
Author: User
Tags autoload spl

One: AutoLoad function

I: In order to instantiate the class object in PHP, it is necessary to find the definition of the class by some means.

Typically, a class is defined in a separate file.

Now we create a folder libs, build files test.php and Test.class.php

Libs/test.php and libs/test.class.php and index.php

1:libs/test.php's Code

1 <? PHP 2 class test{3      Public function __construct () {4         Echo "Loading class libs/test.php<br>"; 5     }6 }

2:libs/test.class.php's Code

1 <? PHP 2 class test{3      Public function __construct () {4         Echo "Loading class libs/test.class.php<br>"; 5     }6 }

3:index.php's Code

1<?PHP2 //set the Mount extension suffix name of the autoload, which can be multiple, separated by commas, preceded by precedence3Spl_autoload_extensions ('. class.php,.php '));4 //Setting Environment Variables5 Set_include_path(Get_include_path(). Path_separator. " libs/");6 //tell PHP to use the AutoLoad function to automatically load classes7 Spl_autoload_register ();8 //can use9 $obj=NewTest ();Ten //output Loading class libs/test.class.php

II: Other methods

__autoload () function and custom load function

1<?PHP2 //the Magic method automatically loads classes without using Spl_autoload_register3 function__autoload ($classname){4     require_once' libs/'.$classname.". Php;5 }6 //custom load classes, which can be automatically called using Spl_autoload_register ("Load")7 functionLoad$classname){8     require_once' libs/'.$classname.". Php;9 }Ten  One NewTest ();//output: Loading class libs/test.php

The load () function can use the Spl_autoload function without the require_once function

1 function Load ($classname) {2     spl_autoload ("libs/").  $classname); 3 }

Iii:autoload () Run process

Second: Other functions

The Splfileinfo class and the Splfileobject class.

1<?PHP2 $file=NewSplfileinfo ("Demo.txt");3 //File creation Time4 Echo"FILE is CREATE at".Date("Y-m-d h:i:s",$file->getctime ()). " <br> ";5 //File modification Time6 Echo"FILE is Modified".Date("Y-m-d h:i:s",$file->getmtime ()). " <br> ";7 //File Size8 Echo"FILE size is".$file->getsize (). "Bytes<br>";9 //Read File contentsTen $FILEOBJ=$file->openfile ("R"); One  while($FILEOBJ-valid ()) { A     //get a line of files -     Echo $FILEOBJ-fgets(); - } the $FILEOBJ=NULL;//close file, necessary - $file=NULL;//close file, necessary

Three: Course Summary

SPL Learning Note (5) Use of---functions

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.