Introduction to 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.
Copy the Code 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)
Copy the Code 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
Copy the Code 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:
Copy the Code 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;
}