Common functions in the PHPSPL standard library, phpspl. Common functions in the PHPSPL standard library are introduced. some functions are provided in the phpsplPHPSPL standard library for processing, such as automatic loading and iterator processing. Spl_autoload_extensions () add common functions in the spl_aut php spl standard library, phpspl
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 */
<? Php
Class Test1
{
}
/* Test2.lib. php */
<? Php
Class Test2
{
}
/* Test. php */
<? 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 */
<? Php
Class Test1
{
}
/* Test. php */
<? 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 */
<? Php
Class Test
{
Public function getFilename ()
{
Echo 'test1. php ';
}
}
/* Test2.lib. php */
<? Php
Class Test
{
Public function getFilename ()
{
Echo 'test2. lib. php ';
}
}
/* Test. php */
<? 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 of common functions in http://www.bkjia.com/PHPjc/997903.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/997903.htmlTechArticlePHP SPL standard library, phpspl php spl standard library provides some functions for processing such as automatic loading, iterator processing, etc. Spl_autoload_extensions () add spl_aut...