Home > Backend Development > PHP Tutorial > Introduction to common functions in the PHP SPL standard library_PHP tutorial

Introduction to common functions in the PHP SPL standard library_PHP tutorial

WBOY
Release: 2016-07-13 09:54:11
Original
855 people have browsed it

Introduction to common functions in the PHP SPL standard library

This article mainly introduces the introduction of common functions in the PHP SPL standard library. This article focuses on spl_autoload_extensions(), spl_autoload_register( ), spl_autoload() three functions, friends in need can refer to them

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

spl_autoload_extensions() adds file extensions that can be loaded by spl_autoload()

spl_autoload_register() registers the function into the SPL __autoload function stack.

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

Other SPL function introduction:

 class_implements — Returns all 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 — Call a user-defined function for each element in the iterator

iterator_count — Count 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 — 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 the 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/998361.htmlTechArticleIntroduction to common functions in the PHP SPL standard library This article mainly introduces an introduction to common functions in the PHP SPL standard library , this article focuses on spl_autoload_extensions(), spl_autoload_register(...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template