Home >Backend Development >PHP Tutorial >Introduction to common functions in the PHP SPL standard library, phpspl_PHP tutorial

Introduction to common functions in the PHP SPL standard library, phpspl_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:54:23898browse

Introduction to common functions in the PHP SPL standard library. phpspl

The PHP SPL standard library provides some functions for processing such as automatic loading, iterator processing, etc.

spl_autoload_extensions() adds file extensions that spl_autoload() can load
spl_autoload_register() registers the function into the SPL __autoload function stack.

Copy code 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 will look for class files in these paths, you can add multiple paths through PATH_SEPARATOR
set_include_path(get_include_path().PATH_SEPARATOR.'libs/');
//No parameters are provided, the default implementation function is spl_autoload()
spl_autoload_register();

$test1 = new Test1();
$test2 = new Test2();

spl_autoload() is the default implementation of __autoload(). It will load files (.php/.inc) in include_path

Copy code 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 registered functions to load files

Copy code 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

Introduction to other SPL functions:
class_implements — Returns all interfaces implemented by the specified class.
class_parents — Returns the parents of the specified class.
class_uses — Return the traits used by the given class
iterator_apply — Call a user-defined function for each element in the iterator
iterator_count — Count the number of elements in an iterator
iterator_to_array — Copy elements in an iterator to an array
spl_autoload_functions — Returns all registered __autoload() functions
spl_autoload_unregister — Unregister the registered __autoload() function
spl_classes — Returns all available SPL classes
spl_object_hash — Returns the hash id of the specified object

If iterator related functions are used:
Copy code The code is as follows:
$iterator = new ArrayIterator (array( 'recipe' => 'pancakes' , 'egg' , 'milk' , 'flour' ));

print_r(iterator_to_array($iterator)); //Convert iterator elements into array
echo iterator_count($iterator); //Calculate the number of iterator elements
print_r(iterator_apply($iterator, 'print_item', array($iterator)));//Call a custom function for each element of the iterator


function print_item(Iterator $iterator)
{
echo strtoupper ( $iterator -> current ()) . "n" ;
Return TRUE ;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/997903.htmlTechArticleIntroduction to common functions in the PHP SPL standard library. phpspl The PHP SPL standard library provides some functions to handle such as Autoloading, iterator handling, etc. spl_autoload_extensions() adds spl_aut...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn