This article mainly introduced the PHP SPL standard library in the common function introduction, this article emphatically explained Spl_autoload_extensions (), Spl_autoload_register (), Spl_autoload () three functions, Need friends can refer to the following
The PHP SPL standard library provides functions for processing such as automatic loading, iterator processing, and so on.
Spl_autoload_extensions () Add spl_autoload () the file name extension that can be loaded
Spl_autoload_register () registers the function into the SPL __autoload function stack.
Copy code code as follows:
/*test1.php*/
Class Test1
{
}
/*test2.lib.php*/
Class Test2
{
}
/*test.php*/
To set the file name extension of a loadable class
Spl_autoload_extensions (". php,.inc.php,.class.php,.lib.php");
Setup Include_path,autoload will look for class files in these path, and you can add multiple path through Path_separator
Set_include_path (Get_include_path (). Path_separator. ' libs/');
No arguments are supplied, the default implementation function is Spl_autoload ()
Spl_autoload_register ();
$test 1 = new Test1 ();
$test 2 = new Test2 ();
Spl_autoload () It is the default implementation of __autoload () that will go to include_path to load the file (. php/.inc)
Copy code code as follows:
/*test1.php*/
Class Test1
{
}
/*test.php*/
Set_include_path (Get_include_path (). Path_separator. ' libs/');
Spl_autoload (' test1 ');
$test 1 = new Test1 ();
Spl_autoload_call () calls all Spl_autoload_register registration functions to load files
Copy code 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 Introduction:
class_implements-returns all the interfaces implemented by the specified class.
class_parents-returns the parent class of the specified class.
Class_uses-return the traits used by the given class
iterator_apply-invokes a user-defined function for each element in the iterator
iterator_count-calculate the number of elements in an iterator
iterator_to_array-copy elements from an iterator to an array
spl_autoload_functions-returns all registered __autoload () functions
spl_autoload_unregister-logout of the registered __autoload () function
spl_classes-returns all available SPL classes
spl_object_hash-returns the hash ID of the specified object
such as iterator related functions use:
Copy code code as follows:
$iterator = new Arrayiterator (Array (' recipe ' => ' pancakes ', ' egg ', ' milk ', ' flour '));
Print_r (Iterator_to_array ($iterator)); Converting an iterator element to an array
echo Iterator_count ($iterator); Count the number of iterator elements
Print_r (iterator_apply ($iterator, ' Print_item ', Array ($iterator));//Call a custom function for the iterator for each element
function Print_item (iterator $iterator)
{
Echo strtoupper ($iterator-> Current ()). "N";
return TRUE;
}