Introduction to common functions in the PHPSPL standard library _ PHP Tutorial

Source: Internet
Author: User
Common functions in the PHPSPL standard library. Common functions in the PHPSPL standard library this article mainly introduces common functions in the PHPSPL standard library. This article focuses on spl_autoload_extensions () and spl_autoload_register (common functions in the php spl standard Library)

This article mainly introduces common functions in the php spl standard library. This article focuses on three functions: spl_autoload_extensions (), spl_autoload_register (), and spl_autoload (). For more information, see

The php spl standard library provides functions for processing, such as automatic loading and iterator processing.

Spl_autoload_extensions () add the file extension that spl_autoload () can load

Spl_autoload_register () registers the function to the SPL _ autoload function stack.

The code is as follows:

/* Test1.php */

  

Class Test1

{

}

/* Test2.lib. php */

  

Class Test2

{

}

/* Test. php */

  

// Set the file extension of the loadable class

Spl_autoload_extensions (". php,. inc. php,. class. php,. lib. php ");

// Set include_path. autoload searches for class files in these paths. you can use PATH_SEPARATOR to add multiple paths.

Set_include_path (get_include_path (). PATH_SEPARATOR. 'libs /');

// No parameters are provided. the default function is spl_autoload ()

Spl_autoload_register ();

$ Test1 = new Test1 ();

$ Test2 = new Test2 ();

Spl_autoload () is the default implementation of _ autoload (). It loads files (. php/. inc) in include_path)

The code is as follows:

/* Test1.php */

  

Class Test1

{

}

/* Test. php */

  

Set_include_path (get_include_path (). PATH_SEPARATOR. 'libs /');

Spl_autoload ('test1 ');

$ Test1 = new Test1 ();

Spl_autoload_call () calls all spl_autoload_register registration functions to load files

The code is as follows:

/* Test1.php */

  

Class Test

{

Public function getFilename ()

{

Echo 'test1. php ';

}

}

/* Test2.lib. php */

  

Class Test

{

Public function getFilename ()

{

Echo 'test2. lib. php ';

}

}

/* Test. php */

  

Function loader ($ classname)

{

If ($ classname = 'test1 '){

Require _ DIR _. '/test1.php ';

}

If ($ classname = 'test2 '){

Require _ DIR _. '/test2.lib. php ';

}

}

Spl_autoload_register ('loader ');

Spl_autoload_call ('test2 ');

$ Test = new Test ();

$ Test-> getFilename (); // test2.lib. php

Other SPL functions:

Class_implements-return all interfaces implemented by the specified class.

Class_parents-return the parent class of the specified class.

Class_uses-Return the traits used by the given class

Iterator_apply-calls a user-defined function for each element in the iterator

Iterator_count-calculate the number of elements in the iterator

Iterator_to_array-copy the elements in the iterator to the array

Spl_autoload_functions-returns all registered _ autoload () functions.

Spl_autoload_unregister-deregister the registered _ autoload () function

Spl_classes-returns all available SPL classes

Spl_object_hash-return the hash id of the specified object

For example, use iterator functions:

The code is as follows:

$ Iterator = new ArrayIterator (array ('receiver' => 'pancaes', 'egg', 'milk', 'flour '));

Print_r (iterator_to_array ($ iterator); // Convert the iterator element into an array

Echo iterator_count ($ iterator); // calculates the number of iterator elements

Print_r (iterator_apply ($ iterator, 'print _ item', array ($ iterator); // calls a custom function for each element of the iterator

Function print_item (Iterator $ iterator)

{

Echo strtoupper ($ iterator-> current (). "\ n ";

Return TRUE;

}

Introduction to common functions in http://www.bkjia.com/PHPjc/998361.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/998361.htmlTechArticlePHP SPL standard library this article mainly introduces common functions in php spl standard library, this article focuses on the introduction of spl_autoload_extensions (), spl_autoload_register (...

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.